Farruh Habibullaev
Personal Blog

Personal Blog

Facade, Strategy, and Template Method Patterns

Facade, Strategy, and Template Method Patterns

Design patterns

Farruh Habibullaev's photo
Farruh Habibullaev
·Nov 1, 2019·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Facade Pattern

The facade is one of the easiest design patterns to implement and belongs to the 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. The facade will be the solution to reduce the complexity, by introducing the simple interface to a complex subsystem.

  • Facade is also used when there are lots of dependencies between the client and concrete implementation of the abstract. Facade objects can reduce this complexity by containing references to those concrete implementations, and delegating requests 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 the behavior patterns family, it is also known as an 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 a shopping mall and purchases 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 who wants to go from home to his office. He can go by many methods, taking a train, driving a car, walking, or riding a bicycle. To implement those behaviors for a particular task, we use a 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 the 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 the template method is we define the general logic in abstract parent class, and let the child class define the specifics.

Let's clarify this with a simple example, imagine we would like to make a cake. The general steps to make a cake are 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.

Let's assume that making dough for all the cakes is 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 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 the java implementation of those examples in my github here.

Thank you for reading!

Share this