模板方法模式

定义:定义一个操作中算法的框架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构即可重定义该算法中的某些特定步骤。

需要实现一套相同流程,只需要子类去继承模板类

 

 

代码:

模板类Cook 

public abstract class Cook {
    public abstract void oil();
    public abstract void egg();
    public abstract void tomato();

    // 定义方法执行顺序,具体步骤延迟到子类完成
    public void doCook() {
        this.oil();
        this.egg();
        this.tomato();
    }
}
MeCook:
public class MeCook extends Cook {

    @Override
    public void oil() {
        System.out.println("I put oil");
    }

    @Override
    public void egg() {
        System.out.println("I put egg");
    }

    @Override
    public void tomato() {
        System.out.println("I put tomato");
    }
}
MasterCook:
public class MasterCook extends Cook {
    @Override
    public void oil() {
        System.out.println("master put oil");
    }

    @Override
    public void egg() {
        System.out.println("master put egg");
    }

    @Override
    public void tomato() {
        System.out.println("master put tomato");
    }
}

测试类:

public class Test1 {
    public static void main(String[] args) {
        MeCook meCook = new MeCook();
        meCook.doCook();
        MasterCook masterCook = new MasterCook();
        masterCook.doCook();
    }
}

运行:

 

 这样我和大厨都执行了一套相同的流程, 但是这样也有缺点,每个子类都要做一样的步骤,假如哪个子类想做少一些步骤呢,这样就不适用了。

所以就有了钩子函数:钩子的作用,让子类去决定这个步骤要不要做

代码:

HookCook :
public abstract class HookCook {
    //钩子方法,在模板方法中使用,子类可以重写这个方法来决定要不要这个步骤
    public boolean isAddTomato() {
        return true;
    }
    public abstract void oil();
    public abstract void egg();
    public abstract void tomato();


    public void doCook() {
        this.oil();
        this.egg();
        if(isAddTomato()) {
            this.tomato();
        }
    }
}
MeCook :
public class MeCook extends HookCook {
    boolean b = true;
    public MeCook() {

    }
    public MeCook(boolean b){
        this.b = b;
    }

    @Override
    public boolean isAddTomato() {
        return this.b;
    }

    @Override
    public void oil() {
        System.out.println("I put oil");
    }

    @Override
    public void egg() {
        System.out.println("I put egg");
    }

    @Override
    public void tomato() {
        System.out.println("I put tomato");
    }
}
MasterCook :
public class MasterCook extends HookCook {

    boolean b = true;
    public MasterCook() {

    }
    public MasterCook(boolean b){
        this.b = b;
    }

    @Override
    public boolean isAddTomato() {
        return this.b;
    }


    @Override
    public void oil() {
        System.out.println("master put oil");
    }

    @Override
    public void egg() {
        System.out.println("master put egg");
    }

    @Override
    public void tomato() {
        System.out.println("master put tomato");
    }
}

测试类:

public class TestHookTemplate {
    public static void main(String[] args) {
        MeCook meCook = new MeCook(true);
        meCook.doCook();
        //大师不需要放西红柿
        MasterCook masterCook = new MasterCook(false);
        masterCook.doCook();
    }
}

结果:

 

posted @ 2020-10-21 21:33  圣金巫灵  阅读(97)  评论(0编辑  收藏  举报