Static and dynamic composition
We can have several possible implementations. The classic way to design this is to differentiate these implementations in subclasses. In this case, we will provide an interface from where our classes will implement this interface.
This solution consists of a static composition. Indeed, once the implementation class of an object is chosen, we can no longer change it. The following diagram is the implementation of an object by heritage:
Another way is to separate the implementation in another object. The implementation parts are managed by an instance of the ConcreteImplementationA
class or by the ConcreteImplementationB
class. This reference is referred by the implementation
attribute. This instance can then be easily substituted by another instance at runtime. This composition is dynamic.
The following UML class diagram shows us clearly how to structure your objects using a dynamic composition. The ConcreteImplementation
class can be switched at runtime, without...