Facade, Strategy and Template Method Patterns

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!




要查看或添加评论,请登录

Farruh Habibullaev的更多文章

  • Good API architecture principles

    Good API architecture principles

    Claimer: This article is purely written from my own experience in software development and design, however in some…

  • Practical Dijkstra's Algorithm

    Practical Dijkstra's Algorithm

    In computer science and discrete mathematics, we have encountered the concept of "single - source shortest path" many…

  • Android Core: Looper, Handler, and HandlerThread

    Android Core: Looper, Handler, and HandlerThread

    I have posted this article for those who don't have any idea how multi-threading architecture in android works, and…

  • OkHttp's Ins and Outs (Part 2)

    OkHttp's Ins and Outs (Part 2)

    This is the second part of OkHttp tutorial. You can find the first part of tutorial in the link here.

    1 条评论
  • OkHttp's Ins and Outs (Part 1)

    OkHttp's Ins and Outs (Part 1)

    This is the first part of OkHttp tutorial. 1.

    4 条评论
  • Creational Design Patterns

    Creational Design Patterns

    Creational design patterns are one of three categories of design patterns (creational, structural, and behavioral…

  • Decorator and Proxy Pattern

    Decorator and Proxy Pattern

    Decorator pattern Decorator pattern is one of the structural design patterns and used to modify the functionality of…

  • S.O.L.I.D Principles in OO programming

    S.O.L.I.D Principles in OO programming

    SOLID is the first five essential principles of object oriented programming for building a well designed, working…

  • Mediator and Command Pattern

    Mediator and Command Pattern

    Mediator Pattern Mediator pattern falls into category of behavioral patterns since it deals with behavior of the…

  • Composite Design Pattern

    Composite Design Pattern

    Composite design patterns falls into one of structural design patterns. Before diving into the composite design…

    2 条评论

社区洞察

其他会员也浏览了