What is the difference between the template method and the strategy patterns?

后端 未结 16 2028
春和景丽
春和景丽 2020-12-07 07:11

Can someone please explain to me what is the difference between the template method pattern and the strategy pattern is?

As far as I can tell they are 99% the same -

相关标签:
16条回答
  • 2020-12-07 07:38

    The main difference between the two is when the concrete algorithm is chosen.

    With the Template method pattern this happens at compile-time by subclassing the template. Each subclass provides a different concrete algorithm by implementing the template's abstract methods. When a client invokes methods of the template's external interface the template calls its abstract methods (its internal interface) as required to invoke the algorithm.

    class ConcreteAlgorithm : AbstractTemplate
    {
        void DoAlgorithm(int datum) {...}
    }
    
    class AbstractTemplate
    {
        void run(int datum) { DoAlgorithm(datum); }
    
        virtual void DoAlgorithm() = 0; // abstract
    }
    

    In contrast, the Strategy pattern allows an algorithm to be chosen at runtime by containment. The concrete algorithms are implemented by separate classes or functions which are passed to the strategy as a parameter to its constructor or to a setter method. Which algorithm is chosen for this parameter can vary dynamically based on the program's state or inputs.

    class ConcreteAlgorithm : IAlgorithm
    {
        void DoAlgorithm(int datum) {...}
    }
    
    class Strategy
    {
        Strategy(IAlgorithm algo) {...}
    
        void run(int datum) { this->algo.DoAlgorithm(datum); }
    }
    

    In summary:

    • Template method pattern: compile-time algorithm selection by subclassing
    • Strategy pattern: run-time algorithm selection by containment
    0 讨论(0)
  • 2020-12-07 07:39


    Similarities

    Strategy and Template method patterns have a lot of similarities between them. Both Strategy and Template method patterns can be used for satisfying the Open-Closed Principle and making the software module easy to extend without changing its code. Both patterns represent separation of generic functionality from the detailed implementation of that functionality. However, they differ a little in terms of granularity they offer.


    Differences

    Here are some of the differences I have observed while studying these two patterns:

    1. In Strategy, the coupling between the client and strategy is more loose whereas in Template Method, the two modules are more tightly coupled.
    2. In Strategy, mostly an interface is used though abstract class can also be used depending on the situation, and concrete class is not used whereas in Template method mostly abstract class or concrete class is used, interface is not used.
    3. In Strategy pattern, generally entire behaviour of the class is represented in terms of an interface, on the other hand, Template method is used for reducing code duplication and the boilerplate code is defined in base framework or abstract class. In Template Method, there can even be a concrete class with default implementation.
    4. In simple words, you can change the entire strategy (algorithm) in Strategy pattern, however, in Template method, only some things change (parts of algorithm) and rest of the things remain unchanged. In Template Method, the invariant steps are implemented in an abstract base class, while the variant steps are either given a default implementation, or no implementation at all. In Template method, the component designer mandates the required steps of an algorithm, and the ordering of the steps, but allows the component client to extend or replace some number of these steps.

    Image is taken from the bitesized blog.

    0 讨论(0)
  • 2020-12-07 07:40

    Strategy Design Pattern

    • Supports composition.
    • Provides you the flexibility to change the behavior of object at runtime.
    • Less coupling between the client code and the solution/algorithm code.

    Template Method Design Pattern

    • Favours inheritance over composition
    • Define algorithm in your base class. Individual pieces of algorithm can be customized in child classes.
    0 讨论(0)
  • 2020-12-07 07:41

    Strategy is exposed as an Interface and template method as the Abstract Class. This is typically used a lot in frameworks. e.g. Spring framework's MessageSource class is a strategy interface for resolving messages. Client uses particular implementation (strategy) of this interface.

    And the abstract implementation of the same interface AbstractMessageSource, which has common implementation of resolving messages and exposes resolveCode() abstract method so that sub-classes can implement them in their ways. AbstractMessageSource is an example of template method.

    http://docs.spring.io/spring/docs/4.1.7.RELEASE/javadoc-api/org/springframework/context/support/AbstractMessageSource.html

    0 讨论(0)
提交回复
热议问题