Difference between template pattern and strategy pattern

foreword

Template mode and strategy mode are often encountered in daily development. What is the difference between these two design modes? Here is a brief summary.

Template mode simple demo

// 抽象模板类
abstract class AbstractClass {
    // 模板方法定义了算法的骨架
    public void templateMethod() {
        // 执行固定的步骤
        step1();
        step2();
        step3();
    }
    
    // 抽象方法,由子类实现具体的步骤
    protected abstract void step1();
    protected abstract void step2();
    
    // 钩子方法,由子类选择性地实现具体的步骤
    protected void step3() {
        // 默认实现
    }
}

// 具体模板类
class ConcreteClass extends AbstractClass {
    protected void step1() {
        System.out.println("执行步骤1");
    }
    
    protected void step2() {
        System.out.println("执行步骤2");
    }
    
    protected void step3() {
        System.out.println("执行步骤3");
    }
}

// 客户端代码
public class TemplatePatternExample {
    public static void main(String[] args) {
        AbstractClass template = new ConcreteClass();
        template.templateMethod();
    }
}

In the above example, the abstract class AbstractClass defines the skeleton of the algorithm, and the overall process of the algorithm is controlled by the templateMethod method, and each specific step is defined by the abstract method and the hook method. The specific subclass ConcreteClass implements specific steps as needed. Clients use the template pattern by creating an instance of a concrete subclass and calling the templateMethod method.

Strategy mode simple demo

// 定义一个接口,表示可互换的算法
interface Strategy {
    void execute();
}

// 实现具体的算法
class ConcreteStrategy1 implements Strategy {
    public void execute() {
        System.out.println("执行策略1");
    }
}

// 实现具体的算法
class ConcreteStrategy2 implements Strategy {
    public void execute() {
        System.out.println("执行策略2");
    }
}

// 客户端代码
public class StrategyPatternExample {
    public static void main(String[] args) {
        // 根据需要选择具体的策略
        Strategy strategy = new ConcreteStrategy1();
        // 执行策略
        strategy.execute();
        
        // 切换到另一个策略
        strategy = new ConcreteStrategy2();
        // 执行策略
        strategy.execute();
    }
}

In the above example, an interface Strategy representing an interchangeable algorithm is defined, and two concrete strategy classes ConcreteStrategy1 and ConcreteStrategy2 are implemented. Clients choose and use different strategies as needed. Use the strategy pattern by creating an instance of a concrete strategy class and calling the execute method.

Summarize

Template Pattern and Strategy Pattern are two commonly used design patterns, and they have some differences in solving different problems.

  • The template pattern is a behavioral design pattern that defines the skeleton of an algorithm, deferring the concrete implementation of some steps to subclasses. The template pattern defines the skeleton of the algorithm in the parent class and then uses abstract methods to expose the variable parts, so that subclasses can implement specific steps according to their own needs. This approach ensures that the structure of the algorithm does not change, but allows custom implementation of specific steps.
  • The Strategy pattern is also a behavioral design pattern, which defines a set of algorithms that can be replaced with each other, and enables these algorithms to vary independently of the clients using the algorithms. The Strategy pattern encapsulates each algorithm in a different class and allows the client to choose which algorithm to use as needed. This approach separates the selection and use of algorithms from the implementation of specific algorithms, providing greater flexibility and scalability.

To sum up, the template pattern focuses on using inheritance to implement the skeleton of the algorithm and allows subclasses to implement specific steps, while the strategy pattern focuses on using composition to select and use different algorithm implementations. The template mode defines the skeleton of the algorithm in the parent class, and the subclass is only responsible for implementing specific steps, while the strategy mode completely leaves the selection and use of the algorithm to the client.

Supongo que te gusta

Origin blog.csdn.net/qq_28165595/article/details/131876747
Recomendado
Clasificación