Reading notes Zen Design Patterns Template Method Pattern

Template Method Pattern

The flow of certain operations in the process defined class good, and the specific steps in the operation of these logic delay to subclasses to achieve, so that subclasses can without changing the operation flow, to modify the specific steps logic.

The method defined flow of operation performed here is called the template method, all of which share subclasses, subclasses diversity can be unfolded by a specific logical step is defined.

In the Template Method pattern, you need to define an abstract parent class (AbstractClass), the parent class is called an abstract template class consists mainly of two methods:

  • Basic Method: also known as the basic operation, a method is needed to achieve sub-class, the method is called in the template;
  • Template Method: can have one or more, the process defined in the flow of execution of some operations, to achieve the basic operation of scheduling, in fixed logic used directly inherited by subclasses. Normally, this method does not allow to be rewritten, so use finalkeywords modification.

Template Method pattern composed of

Abstract template class

Which method to perform process design, and template method for determining the parent class has

public abstract class AbstractClass{
    // 基本方法1
    protected abstract void doSomething();
    // 基本方法2
    protected abstract void doAnything();
    // 模板方法1, 设计操作的执行流程
    public void templateMethod1() {
        this.doAnything();
        this.doSomething();
    }
    // 模板方法2, 设计操作的执行流程
    public void templateMethod2() {
        this.doSomething();
        this.doAnything();
    }
}

Specific template class

The basic method of the parent class subclasses implement according to specific needs

public class ConcreteClass extends AbstractClass {
    // 根据需求实现基本方法中的逻辑
    protected abstract void doSomething() {
        System.out.println("我是具体的实现,这是我的逻辑,我做点啥呢...");
    }
    // 根据需求实现基本方法中的逻辑
    protected abstract void doAnything() {
        System.out.println("我是具体的实现,这是我的逻辑,我想做啥就做啥...");
    }
}

Scene class

Test Method for the template

public class Client {
    public static void main(String[] args) {
        AbstractClass myClass = new ConcreteClass();
        // 调用模板方法
        myClass.templateMethod1();
        myClass.templateMethod1();
    }
}

Advantages and disadvantages of the template method pattern

advantage

  • Extract the public part of the code, easy to maintain, subclass only need to care about their characteristics do not care about the specific behavior, behavior is controlled by the same parent

Shortcoming

  • Results of the subclasses of the parent class control, reading of the code may be more difficult

Template Method pattern of usage scenarios

  • A plurality of sub-class has public methods, and performs substantially the same as the logic of the method
  • Important, complex algorithms, the core algorithm can be designed as a template method, details of other related methods to be implemented by subclasses
  • When the code is reconstructed, the same code can be drawn into the parent class

Guess you like

Origin www.cnblogs.com/sasworld/p/11620031.html