Template Method Design Pattern

 

 

 

 

 

 

Template Method Design Pattern

Rules of thumb

  • Strategy is like Template Method except in its granularity粒度.
  • Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm.
  • Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class.
  • Factory Method is a specialization of Template Method.

 

Template Method

 Relations with Other Patterns

  • Factory Method is a specialization of Template Method. At the same time, a Factory Method may serve as a step in a large Template Method.

  • Template Method is based on inheritance: it lets you alter parts of an algorithm by extending those parts in subclasses. Strategy is based on composition: you can alter parts of the object’s behavior by supplying it with different strategies that correspond to that behavior. Template Method works at the class level, so it’s static. Strategy works on the object level, letting you switch behaviors at runtime.

 

posted @ 2020-06-16 14:21  ChuckLu  阅读(139)  评论(0编辑  收藏  举报