OOP: Which class should own a method? [closed]

subjective 🙂 but honestly, I’d go with the Information Expert Pattern and say something like library.lend(item, patron) The library contains the information about the items it has (perhaps in its catalog). The library lends the item to the patron (which it knows because it registers them) Not sure how your instructor sees this, but this … Read more

Why is ASP.NET Core’s Startup class not an interface or abstract class?

There are several reasons why its done the way its done. One of the more obvious reasons is, because you can inject services into Configure method, such as public void Configure(IAppBuilder app, IMyService myService) { myService.DoSomething(); } Obviously, you can’t do that with interfaces, abstract classes or inheritence. The second reason why its done by … Read more

Classes to avoid (code complete)

Class names like InvoiceReader, PriceCalculator, MessageBuilder, ArticleReader, InvoiceReader are not actually verb names. They are really “noun agent-noun” class names. See agent nouns. A verb class name would be something like Validate, Operate, Manage etc. Obviously these are better used as methods and would be quite awkward as class names. The biggest problem with “noun … Read more

Using a class’ __new__ method as a Factory: __init__ gets called twice

When you construct an object Python calls its __new__ method to create the object then calls __init__ on the object that is returned. When you create the object from inside __new__ by calling Triangle() that will result in further calls to __new__ and __init__. What you should do is: class Shape(object): def __new__(cls, desc): if … Read more

OO Javascript constructor pattern: neo-classical vs prototypal

This looks like the non-singleton version of the module pattern, whereby private variables can be simulated by taking advantage of JavaScript’s “closures”. I like it (kinda…). But I don’t really see the advantage in private variables done in this way, especially when it means that any new methods added (after initialisation) do not have access … Read more

why is java.lang.Throwable a class?

Here’s how James Gosling explained his decision: Java Developer Connection Program: Why is Throwable not an interface? The name kind of suggests it should have been. Being able to catch for types, that is, something like try {} catch (<some interface or class>), instead of only classes. That would make [the] Java [programming language] much … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)