what's the difference between the patterns Strategy, Visitor and Template Method?

前端 未结 2 766
广开言路
广开言路 2021-01-31 05:45

I\'m in a class where we just learned about these design patterns. However I couldn\'t see any difference between them. They sound just like the same, creating concrete classes

2条回答
  •  伪装坚强ぢ
    2021-01-31 06:39

    Commonalities:

    1. Strategy, Template method and Visitor : All three patterns are categorized as behavioural patterns.

    Differences:

    1. Template method uses Inheritance and Strategy uses composition
    2. The Template method implemented by the base class should not be overridden. In this way, the structure of the algorithm is controlled by the super class, and the details are implemented in the sub classes
    3. Strategy encapsulates the algorithm behind an interface, which provide us ability to change the algorithm at run time
    4. Visitor pattern is used to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class
    5. If there is a change in implementation of Operation, we have to just change Visitor class instead of touching all other objects.

    Have a look at Template method , Strategy and Visitor and Sourcemaking articles for better understanding.

    Related posts:

    When should I use the Visitor Design Pattern?

    Real World Example of the Strategy Pattern

    Template design pattern in JDK, could not find a method defining set of methods to be executed in order

提交回复
热议问题