美文网首页
模版模式

模版模式

作者: Stroman | 来源:发表于2018-05-27 19:37 被阅读29次

类图

模版模式.png

实现

调用

package com.company;

public class Main {

    public static void main(String[] args) {
    // write your code here
        GeneralClass general = new GeneralClass();
        general.doIt();

        System.out.println("++++++++++++++++++++");

        GeneralClass elephant = new ElephantClass();
        elephant.doIt();

        System.out.println("++++++++++++++++++++");

        GeneralClass apple = new AppleClass();
        apple.doIt();
    }
}

输出

把冰箱门打开
把某东西装进去
把冰箱门带上
++++++++++++++++++++
把冰箱门打开
把大象装进去
把冰箱门带上
++++++++++++++++++++
把冰箱门打开
把苹果装进去
把冰箱门带上

Process finished with exit code 0

把某东西装进冰箱的类

package com.company;

/**
 * 把某东西装冰箱
 */
public class GeneralClass {
    protected void openFridge() {
        System.out.println("把冰箱门打开");
    }

    protected void closeFridge() {
        System.out.println("把冰箱门带上");
    }

    protected void putSomethingIn() {
        System.out.println("把某东西装进去");
    }

    /**
     * 这个就是那个hook方法。
     * 在这里我不重写它,所此处用final修饰。
     */
    public final void doIt() {
        this.openFridge();
        this.putSomethingIn();
        this.closeFridge();
    }
}

把大象装冰箱

package com.company;

public class ElephantClass extends GeneralClass {
    @Override
    protected void putSomethingIn() {
        System.out.println("把大象装进去");
    }
}

把苹果装冰箱

package com.company;

public class AppleClass extends GeneralClass {
    @Override
    protected void putSomethingIn() {
        System.out.println("把苹果装进去");
    }
}

The Template Method Pattern——Encapsulating Algorithm

相关文章

  • 设计模式-模版方法模式

    设计模式-模版方法模式 定义 模版方法模式(Template Method Pattern)又叫模版模式,是指定义...

  • 设计模式[14]-模版方法模式-Template Method

    1.模版方法模式简介 模版方法模式(Template Method Pattern)是行为型(Behavioral...

  • 设计模式之模版方法模式

    模版方法模式 模版方法是一种只需使用继承就可以实现的非常简单的模式模版方法模式由两部分结构组成,第一部分是抽象父类...

  • 设计模式之模版方法模式

    模版方法模式 模版方法是一种只需使用继承就可以实现的非常简单的模式模版方法模式由两部分结构组成,第一部分是抽象父类...

  • 模版方法模式

    模版方法模式 一、什么是模版方法模式 模板模式 :解决某类事情的步骤有些是固定的,有些是会发生变化的,那么这时候我...

  • 设计模式之Template模式(模版模式)

    1 模式简介 1.1 模版方法模式的定义:模版方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模...

  • 模版方法模式

    通俗讲,模版模式就是将通用的上升到父类中,个性化的功能由各个子类完成.代码的复用是模版模式主要解决的.

  • 模版模式

    类图 模版模式.png 实现 调用 输出 把某东西装进冰箱的类 把大象装冰箱 把苹果装冰箱 The Templat...

  • 模版方法模式

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

  • 模版方法模式

网友评论

      本文标题:模版模式

      本文链接:https://www.haomeiwen.com/subject/tjnbjftx.html