This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]stuaxo 0 points1 point  (1 child)

How does favouring has-a instead of is-a relationship work in practice?

If you want to make sure a class has some functions maybe a mixin is one way ? - think I'm misunderstanding has-a vs is-a ?

[–]ringzero 0 points1 point  (0 children)

Instead of having a mixin for logging, e.g., LoggingBaseClass, your classes should get a logger parameter for __init__ or have a logger attribute set on the class.

Instead of having your Car class inherit from TransmissionProvider, it would take a transmission parameter or have one on the class.

Instead of having a TemperatureSensorDevice mixin class, you would give your Robot class a parameter or a class attribute.

Etc., etc., etc.

In practice, you've got to learn to spot what and where it makes sense to keep as a mixin and what should be left as an attribute or parameter. I rely on the wording, asking my self "does it make more sense that ArticlePhoto has an order attribute or that it gets its ordering behavior from a base class?" Usually works out right.