We've put up a new article
. Summary follows:
Polymorphism is the main mechanism to create maintainable object-oriented code. Client code uses other code through an abstract interface, and as long as we don't break the behavioral contract, we can change the implementation behind that interface at will.
You will only achieve real maintainability if client code has dependencies to other code through an abstract interface and nothing else
Unfortunately creational logic will - by its very nature - break this rule
. When you create an instance, at some point you will
need call the constructor on the actual implementing class. This creates a dependency that prevents you from changing the implementation transparently, effectively cancelling the advantages of polymorphism.
The solution to the creational problem is to protect the abstraction by hiding the creational logic
from the client code. Dependency injection is the ideal solution, in this case the client does not use creational logic at all. But in its turn the injecting code needs to create real instances.
This article is the first of a series of three: this article will illustrate how the creational problem manifests itself, the second will deal with some advanced implementations of creational code, and the third will deal with dependency injection.