Poor man's OOP approach

Isn’t OOP all about reusing code?

KISS principle, DRY principle and so on all amount to reusing code to write less code to be able to test, support and refactor less code. Best way to re-use code? Interfaces. No, inheritance. I don’t know and will admit it.

I make a practical reason of it (aka “the average developer approach to OOP”)

Instead of entering the fight about the best way to re-use code via interfaces or inheritance I take a simpler approach based on the idea that both interfaces and inheritance are implementation details the language offers and cover a concept synthesized by the sentence that two objects are about the same thing.

  • if I know that each object implementing a method will have to write it from scratch then I use an interface like, say, the init function in a singleton class
  • if I know that each object implementing a method will have almost the same implementation than I use class inheritance like, say, setView in a controller class

Today’s “gotcha” about interfaces

I thought interfaces works pretty well in modelling aspects of an object that are more related to its implementation and not its role. Think of the Singleton pattern. It’s an implementation choice, the way I get hold of an instance of an object, it does not model what the object does. It only makes sense that Singleton is interface.

I think of objects like shops

You inherit the core-business by your old father and then adapt it to the current situation. The core business is inheritance because a grocery store will always be a grocery store.
Adapting are interfaces because I have to adapt the old grocery store to the new neighbourhood.