Template Method

Introduction

The template method pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

According to above description, we can learn that factory method pattern is a special version of template method as it lets subclass make a decision to instantiate which class.

Example

Taking the same example in command pattern, this time I use template pattern to implement worker class. For different workers, they have the same workflow, that is read request from master process, analyse request, excute request and return result.