Template Method Pattern
Image credits: freepik.com

Template Method Pattern

What is Template Method?

  • It is behavioural design pattern.
  • It defines the skeleton of an algorithm in abstract superclass. This is like high-level steps/contract defined in superclass. This contains either abstract methods(subclasses are responsible for providing concrete implementation) or hook methods(normally having default/empty implementation in place. Hooks may or may not get overridden by subclasses).
  • Intent behind this design pattern is to provide/define the skeleton with flexibility for different concrete implementations out of it without changing the overall structure.
  • Algorithm could be executed by any concrete class(as long as they it adheres to contract/algorithm of abstract superclass) which ultimately provides different behaviour/flavour.

Real world analogy:

NOTE: This is to show analogy with Template method design pattern and does not cover/reflect full house building process.

House construction method has predefined set of steps(skeleton) to be followed. Following is abstract super class.

(1)Site preparation

(2)Foundation set up

(3)Framing

(4)Setup doors

(5)Setup windows

(6)Roof setup

(7)Flooring

(8)Painting

Now, individual house construction(concrete implementation of different house) can follow this skeleton(Template method) with overriding particular step(s) to give different look/feel/architecture for different house.

Example in software/apps development:

  • Consider scenario of Authentication algorithm/process for mobile application.
  • There could be abstract superclass for authentication steps and client can have concrete authentication implementation for PIN, TouchID and FaceID respectively.
  • Sample code in swift: https://github.com/ParthContractor/TemplateMethod

Benefits:

  • Code reusability and standard steps/skeleton to be followed.
  • Flexibility of different concrete implementations.
  • Beneficial only if there is standard pattern/skeleton and different flavours/behaviour possibility in concrete subclasses.

Drawbacks:

  • Possibility to break “Liskov Substitution Principle”
  • Sometimes confusing and difficult to manage if one does not understand the abstract superclass(or abstract protocol/interface) properly.
  • Changes can affect overall implementation.

Comparison with Strategy design pattern:

  • Template method depends upon single algorithm in abstract super class and allowing concrete subclasses to override part of steps.
  • However, Strategy design pattern enables selection of algorithm at runtime. Different strategy is applied to object at runtime to achieve its specific behaviour.

Other interesting articles/reference materials:

Summary:

Template Method pattern is convenient when your algorithm is well defined and you have plausible flavours of concrete implementation keeping the same steps and the order of algorithm. Concrete implementation extends the abstract superclass without changing overall structure of algorithm.

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

社区洞察

其他会员也浏览了