oop

How to decouple a class from it's attributes when the methods of the attributes need to modify state of the owning class?

给你一囗甜甜゛ 提交于 2021-01-29 15:38:01
问题 How do I decouple a class from its attributes when methods of the attributes need to modify the state of the owning class? Alternatively, how can I redesign the architecture so that this isn't a problem? This question is a bit abstract, but I keep coming across this problem time and time again. I spend a lot of time designing my code base so that my classes are "high cohesion and low coupling", but then as the code evolves over time, they end up becoming more closely coupled. I'll give the

Angular 2 how to extend class which has dependency injections

喜你入骨 提交于 2021-01-29 14:44:19
问题 i have the following class which i want to extend. export class AngularComponent { constructor( private r: Renderer2, private editorService: AngularEditorService, @Inject(DOCUMENT) private doc: any, private sanitizer: DomSanitizer, private cdRef: ChangeDetectorRef, @Attribute("tabindex") defaultTabIndex: string, @Attribute("autofocus") private autoFocus: any ) } So i create the following class: export class EnhancedComponent extends AngularComponent{ constructor(private r: Renderer2, private

Is the observer object oriented design pattern just a form of implementing a callback?

萝らか妹 提交于 2021-01-29 11:07:17
问题 I was recently asked what is the difference between a callback and an observer in the Observer OO design pattern. My understanding is that callback is any executable code passed as an argument to a function which is called when a specific event happens. It may be implemented in different forms in different programming languages, such as function pointers, anonymous functions and observers/listeners in the object-oriented paradigm. Developers usually implement callback registration

How to avoid product duplicates in shopping cart

筅森魡賤 提交于 2021-01-29 09:54:15
问题 I'm building an eCommerce project but I'm struggling to avoid duplicates showing on the cart side drawer. Ideally, if a user adds a new product that exists in the cart, I want to increment the quantity instead of listing a new html element. I'm using OOP for this project. See below result in UI. Please see code below: HTML <!-- Cart Drawer --> <div class="cart-drawer" id="side-cart"> <div class="cart-inner"> <button type="button" id="cart-drawer-close" class="cart-drawer-close" > <i class=

Is Inheritance in Python canonically the same as in other languages? A comparative example with Java

好久不见. 提交于 2021-01-29 09:47:54
问题 I have been puzzling today, as (simplified) python code seemed to raise an exception. Here's the code in python : class AwinBaseOperator: def __init__(self): self.data = self.get_data(query_url='test') def get_data(self, query_url): print('Parent called with ', repr(query_url)) class AwinTransactionOperator(AwinBaseOperator): def __init__(self): super().__init__() def get_data(self): print('Child called') print(AwinTransactionOperator()) This returns: Child called with 'test.' It means the

Java: Iterator is supposed to always return the same object, but should change it between calls of next()

邮差的信 提交于 2021-01-29 09:36:49
问题 The Iterator I'm writing is supposed to iterate through all fixed-sized connected subgraphs of another given graph. As these subgraphs are all very similar (only minor changes in a search-tree based algorithm), I don't want to create a new object to return on each call of next() because of runtime concerns. Therefore I have an object subGraph that is returned every time, but is changed by a function update() between calls of next() , so that the object works like a view of the current state

Understanding Mutability and Multiple Variable Assignment to Class Objects in Python

五迷三道 提交于 2021-01-29 06:49:35
问题 I'm looking for some clarification regarding mutability and class objects. From what I understand, variables in Python are about assigning a variable name to an object. If that object is immutable then when we set two variables to the same object, it'll be two separate copies (e.g. a = b = 3 so a changing to 4 will not affect b because 3 is a number, an example of an immutable object). However, if an object is mutable, then changing the value in one variable assignment will naturally change

PHP function return value, how to check

99封情书 提交于 2021-01-29 05:58:42
问题 First a simple example: function doReturnSomething() { // some logic if ($resultFromLogic) { return Default_Model_Something; } return false; } As you can see, this functions returns a model or false. Now, let's call this function from some place else: $something = doReturnSomething(); No, I want to put a if-statement to check the variable $something. Let's say: if (false !== $something) {} or if ($something instanceof Default_Model_Something) {} or ... Is there a "best practice" for this

Where should we use fetch_assoc method?

冷暖自知 提交于 2021-01-29 05:14:56
问题 I started this little blog project and I think I have just made a mistake by not including fetch_assoc method. This is the code: $data = new database(); $sql = "SELECT * FROM post "; $article = $data->select($sql); foreach ($article as $value) : ?> <div class="blog-post"> <h2 class="blog-post-title"><?= $value["blog_title"]; ?></h2> and this is how the select methode look like: public function select($sql){ $result = $this->con->query($sql) or die($this->con->error.__LINE__); if ($result->num

Any disadvantage to creating a class which uses methods which return a Task<T> but are synchronous methods over methods returning T

柔情痞子 提交于 2021-01-29 04:43:18
问题 Let's say we have this class - MyClass MyClass has methods that convert different types of lists to XDocuments As an example we'll just look at one method, Convert I could write this method as: public XDocument Convert<T>(IList<T> myList) { XDocument doc = new XDocument("root"); //Do some synchronous work return doc; } Or I could write it like the following: public Task<XDocument> Convert<T>(IList<T> myList) { XDocument doc = new XDocument("root"); //Do some synchronous work return Task