美文网首页
模板模式

模板模式

作者: keith666 | 来源:发表于2016-05-21 15:58 被阅读19次

Intent

  • Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
  • Base class declares algorithm 'placeholders', and derived classes implement the placeholders.

Structure

template by keith
  1. 代码:
public class Template {
    abstract class Beverage {
        // template method, make it final so that it can not be redefined
        final void prepareRecipe() {
            boilWater();
            brew();
            pourInCup();
            if (wantCondiments())
                addCondiments();
        }

        abstract void brew();

        abstract void addCondiments();

        void boilWater() {
            System.out.println("Boiling water");
        }

        void pourInCup() {
            System.out.println("Pouring into a cup");
        }

        // hook method, it is up to subclass to decide whether to override it or not
        // the hook method make the algorithm structure more flexible
        boolean wantCondiments() {
            return true;
        }
    }
    class Coffee extends Beverage {

        @Override
        void brew() {
            System.out.println("Dripping Coffee through filter");
        }

        @Override
        void addCondiments() {
            System.out.println("Adding Sugar and Milk");
        }
    }
    class Tea extends Beverage {

        @Override
        void brew() {
            System.out.println("Steeping the tea");
        }

        @Override
        void addCondiments() {
            System.out.println("Adding Lemon");
        }

        @Override
        boolean wantCondiments() {
            return false;
        }
    }
    private void test() {
        Coffee coffee = new Coffee();
        Tea tea = new Tea();

        coffee.prepareRecipe();
        tea.prepareRecipe();
    }
    public static void main(String[] args) {
        Template template = new Template();
        template.test();
    }
}
  1. Output
Boiling water
Dripping Coffee through filter
Pouring into a cup
Adding Sugar and Milk
Boiling water
Steeping the tea
Pouring into a cup

Refenrence

  1. Design Patterns
  2. 设计模式

相关文章

  • 11.8设计模式-模板模式-详解

    设计模式-模式模式 模板方法模式详解 模板方法模式在android中的实际运用 1.模板方法模式详解 2.模板方法...

  • 第5章 -行为型模式-模板方法模式

    一、模板方法模式的简介 二、模板方法模式的优点 三、模板方法模式的应用场景 四、模板方法模式的实例

  • 模板方法模式

    模板方法模式 模板方法模式的定义 模板方法模式(Template Method Pattern)是如此简单,以致让...

  • 设计模式系列-模板方法模式

    JAVA设计模式系列: 单例模式 观察者模式 模板方法模式 模板方法模式 定义 模板方法模式在一个方法中定义了算法...

  • 设计模式(行为型)-- 模板模式

    模板模式的原理与实现 模板模式,全称是模板方法设计模式,英文是 Template Method Design Pa...

  • 行为型-Template

    模板模式的原理与实现 模板模式,全称是模板方法设计模式,英文是 Template Method Design Pa...

  • 行为型 模板模式(文末有项目连接)

    1:模板方式解决的问题(先了解) 2:模板模式的原理与实现 3:模板模式核心代码 4:模板模式复用例子(Input...

  • 模板模式,也是解耦算法的吗?

    模板模式,和算法有什么关系呢? 模板模式,在什么场景使用呢? 模板模式(Template Pattern),定义一...

  • 模板方法模式

    一、模板方法模式介绍 二、模板方法模式代码实例

  • 58 - 模板模式

    本文来学习另外一种行为型设计模式,模板模式。模板模式主要是用来解决复用和扩展两个问题 模板模式的原理与实现 模板模...

网友评论

      本文标题:模板模式

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