And being able to change a label only once is more important than software that is easier to maintain?
Ah, but then as a developer I need to know about the child class. With all the inheritance you lose the ability of local reasoning of a single class, everything you do requires global reasoning over multiple classes, which makes things so much harder.
I don’t understand what you’re saying here.
And propose a third option: extract common functionality into a single class, and then create two classes that both get the common functionality injected.
Of course there are examples of where inheritence does work, and this is one of them. That doesn’t mean it always works, and indeed in a lot of cases it’s being used where it doesn’t fit. Classic example being the Shape
concept a lot of people go to to explain inheritance. It may seem reasonable to have methods setWidth
and setHeight
on there, but then what do you do with squares? Since squares have the requirement width = height, when I call setWidth
on it, the height should be set as well. But looking from the interface Shape
that’s totally unreasonable behaviour (and a violation of the Liskov Substitution Principle).
Depends on the kind of class If it’s a service like class than it would be private property, no getters, no setters. In that case it’s just a service class using another service class. Nobody needs to know it’s using it, nobody should care.
In case of Entity like objects I have private properties, maybe a getter if it’s needed, and some kind of setter, but mostly a bit more intelligent than just “change this property”. For example I may have properties for street name and house number, city, postal code, but the setter would require all of these, since most of the time they all change together.
As for the disadvantage of LoD, let me add some emphasis:
Again, it depends. Sometimes it can be worth your while not to adhere to LoD, but it’s important to recognise you’re doing that and you also know the reasons why you’re doing that.
As always, a fully dogmatic approach where everything must adhere to LoD will result in a horrible mess, as with any law / principle / pattern / etc.