Facade, Strategy and Template Method Patterns
Facade Pattern
Facade is one of the easiest design patterns to implement, and belong to structural design pattern family. We can integrate the facade pattern in the following design problems:
- In order to reduce complexity, we divide the system into subsystems according to SRP (Single Responsibility Principles). However, as the subsystems evolve, they become increasingly more complex. Facade will be the solution to reduce the complexity, by introducing the simple interface to complex subsystem.
- Facade is also used when there are lots of dependencies between the client and concrete implementation of abstract. Facade object can reduce this complexity by containing the references to those concrete implementations, and delegating request to relevant implementation.
- In order to layer the subsystem, the facade can provide the entry point to those subsystems.
Java implemented example is given in my github here.
Strategy Pattern
Strategy pattern belongs to behavior patterns family, it is also known as a algorithmic. It is used to solve the following design problems:
- It is used when we have many algorithms for a specific task and the client decides the actual implementation to be used at the runtime
- When there are multiple solutions to the same particular problem, and one must be selected at the runtime.
For example, a customer goes to chopping mall and purchase a bag of bananas. In order to pay for the product, the customer goes to the counter. The customer has options; payment by either credit card, debit card or cash. To do realization of this payment process, we need to implement the strategy pattern.
Or another example, a person wants to go from home to his office. He can go by many methods, taking a train, driving a car, by walking or by riding a bicycle. To implement those behaviors for a particular task, we use strategy pattern.
For all the examples above, you can find the implementations in my github here.
Template Method Design Pattern
Template design pattern also falls into category of behavior pattern. In many ways, it is very similar to the strategy pattern. In the following intents, the template method is used:
- It defines sequential steps of algorithm execution for a particular task. Subclasses are allowed to override the steps but not allowed to change the sequence.
- One of the key points of template method is we define the general logic in abstract parent class, and let the child class define the specifics.
Lets clarify this with simple example, imagine we would like to make a cake. The general steps to make a cake is to make dough (1), prepare ingredients (2), combine the dough with ingredients (3) and cook them all (4). The important point here is we can't change the order of execution. Without making the dough, we can't cook them.
Lets assume that making a dough for all the cakes are the same, no matter what kind of cakes they are. We can provide the basic implementation for it. If subclass would like to override them, they can but since it is a common for all, it is not needed in most cases.
However, we need to make sure people can't override the template method, in Java we can achieve this by final keyword.
You may find java implementation of those examples in my github here.
Thank you for reading!