Template Method pattern (TemplateMethod)

Template design pattern is defined in the book:

  Definition of a skeleton algorithm operation delay some steps to subclasses, subclasses such that the template method may not change the structure of certain steps of the algorithm of the algorithm to redefine.

  Popular point is understood: completing a task, a fixed number of steps, each step depending on the object, to achieve different levels of detail; can be defined in a parent class of the overall process is completed things, according completion event steps needed to call its implementation of each step. Specific realization of each step, performed by the subclass.

Online mode to find a template class diagram:

Abstract parent class (AbstractClass): implement a template method to define the backbone of the algorithm.

Concrete classes (ConcreteClass): abstract class implements the abstract method, i.e., the specific implementation details of different objects.

Template Method mode is intended, the definition of a skeleton of the algorithm operation, but the delay will achieve some of the steps to subclasses. Template Method lets subclasses may not change the structure of certain steps of an algorithm of the algorithm to redefine. Our most common is the Spring framework of various Template.

Practical examples 

We had a cake shop, cake shop not only sell one kind of cake Yeah, so we decided to sell the cream cake, cheese cake and mousse cake. Three kinds of cake in the same way as the production, include something on modeling, painting and baking cakes. It is possible to define an abstract model of cake

Then it can be mass produced three kinds of cake

As a result, not only can be mass produced three kinds of cake, but if there is expansion in the future, only need to inherit abstract methods of cake on it, very convenient, we do business every day more and more money. Suddenly one day, we found the market there is a simple small cakes sell well, this is a simple cake bake molding can sell, what ingredients do not need to apply, because the simple production of large sales, this breed is very profitable, so we also want to produce this cake. But we found a problem, the cake is the definition of abstract painting abstract method, that the expansion of this cake is a must to achieve smear method, which is very child chicken egg pain. How to do? We can modify the template to the original template with hooks.

When a small cake to control whether applied by flag, the rest of the existing cake making does not require any modifications can be carried out as usual.

Finally put all source code

/**
 * 类说明:抽象蛋糕模型
 */
public abstract class AbstractCake {
    protected abstract void shape();
    protected abstract void apply();
    protected abstract void brake();

    /*模板方法*/
//    public final void run(){
//        this.shape();
//        this.apply();
//        this.brake();
//    }

    /*模板方法*/
    public final void run(){
        this.shape();
        if(this.shouldApply()){
            this.apply();
        }
        this.brake();
    }

    protected boolean shouldApply(){
        return true;
    }
}
/**
 * 类说明:芝士蛋糕
 */
public class CheeseCake  extends AbstractCake {

    @Override
    protected void shape() {
        System.out.println("芝士蛋糕造型");
    }

    @Override
    protected void apply() {
        System.out.println("芝士蛋糕涂抹");
    }

    @Override
    protected void brake() {
        System.out.println("芝士蛋糕烘焙");
    }
}


/**
 * 类说明:奶油蛋糕
 */
public class CreamCake extends AbstractCake {
    @Override
    protected void shape() {
        System.out.println("奶油蛋糕造型");
    }

    @Override
    protected void apply() {
        System.out.println("奶油蛋糕涂抹");
    }

    @Override
    protected void brake() {
        System.out.println("奶油蛋糕烘焙");
    }
}

/**
 * 类说明:
 */
public class MouseCake extends AbstractCake {

    @Override
    protected void shape() {
        System.out.println("慕斯蛋糕造型");
    }

    @Override
    protected void apply() {
        System.out.println("慕斯蛋糕涂抹");
    }

    @Override
    protected void brake() {
        System.out.println("慕斯蛋糕烘焙");
    }
}

/**
 * 类说明:小蛋糕
 */
public class SmallCake extends AbstractCake {

    private boolean flag = false;
    public void setFlag(boolean shouldApply){
        flag = shouldApply;
    }
    @Override
    protected boolean shouldApply() {
        return this.flag;
    }
    @Override
    protected void shape() {
        System.out.println("小蛋糕造型");
    }

    @Override
    protected void apply() {
        System.out.println("小蛋糕涂抹");
    }

    @Override
    protected void brake() {
        System.out.println("小蛋糕烘焙");
    }

}
/**
 * 类说明:生产蛋糕
 */
public class MakeCake {
    public static void main(String[] args) {
        AbstractCake cake = new CheeseCake();
        AbstractCake cake2 = new CreamCake();
        //AbstractCake cake = new MouseCake();
        cake.run();

        AbstractCake smalCake = new SmallCake();
        smalCake.run();

    }
}

 

 

 

 

 

 

Published 18 original articles · won praise 4 · Views 144

Guess you like

Origin blog.csdn.net/weixin_42081445/article/details/105018869