April 2018
Intermediate to advanced
300 pages
7h 41m
English
The third approach is composition, and this can be achieved using the Strategy pattern. With this approach, the client code is dependent on the abstraction, and the actual implementation is encapsulated in a separate class which is injected into the class exposed to the client.
Let's look into the following example that implements the strategy pattern. The basic requirement is to send messages that could be either emails or SMSes, and we need to construct it in a way so that new message types can be added in the future without any modification to the main class:

As per the strategy pattern, we have one MessageStrategy abstract ...