每天一个设计模式之模板方法模式(Template Method Pattern)

所谓的模板模式就是基类(抽象类)提供出定义好的一个模板(空实现+默认实现),子类按照模板封装好的顺序去填充模板内方法的实现。

一、UML类图

每天一个设计模式之模板方法模式(Template Method Pattern)_第1张图片
图中,templateMethod是对子类暴露出的方法,它封装了一些列方法执行的步骤,可以对模板内某些方法提供默认实现,子类根据需要覆盖某些非默认实现的方法。

二、代码示例

框架类(提供模板)

public abstract class House {
    protected final void constructBase() {
        System.out.println("Base has been constructed.");
    }
    protected final void constructRoof() {
        System.out.println("Roof has been constructed.");
    }
    protected abstract void constructWalls();
    protected abstract void constructWindows();
    protected abstract void constructDoors();
    protected abstract void paintHouse();
    protected abstract void decorateHouse();

    public final void buildHouse() {
        constructBase();
        constructRoof();
        constructWalls();
        constructWindows();
        constructDoors();
        paintHouse();
        decorateHouse();
    }
}

具体类1

public class ConcreteWallHouse extends House{
    @Override
    public void constructWalls() {
        System.out.println("Constructing Concrete Wall for my house.");
    }

    @Override
    public void constructWindows() {
        System.out.println("Constructing windows for Concrete wall house.");
    }

    @Override
    public void constructDoors() {
        System.out.println("Constructing doors for Concrete wall house.");
    }

    @Override
    public void paintHouse() {
        System.out.println("Printing Concrete wall house.");
    }

    @Override
    public void decorateHouse() {
        System.out.println("Decorating Concrete wall house.");
    }
}

具体类2

public class GlassWallHouse extends House{
    @Override
    public void constructWalls() {
        System.out.println("Constructing Glass Wall for my house.");
    }

    @Override
    public void constructWindows() {
        System.out.println("Constructing windows for glass wall house.");
    }

    @Override
    public void constructDoors() {
        System.out.println("Constructing doors for glass wall house.");
    }

    @Override
    public void paintHouse() {
        System.out.println("Printing glass wall house.");
    }

    @Override
    public void decorateHouse() {
        System.out.println("Decorating glass wall house.");
    }
}

客户类

public class Client {
    public static void main(String[] args) {
        House house = new ConcreteWallHouse();
        house.buildHouse();

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

        house = new GlassWallHouse();
        house.buildHouse();
    }
}

运行结果

Base has been constructed.
Roof has been constructed.
Constructing Concrete Wall for my house.
Constructing windows for Concrete wall house.
Constructing doors for Concrete wall house.
Printing Concrete wall house.
Decorating Concrete wall house.
------------------------------
Base has been constructed.
Roof has been constructed.
Constructing Glass Wall for my house.
Constructing windows for glass wall house.
Constructing doors for glass wall house.
Printing glass wall house.
Decorating glass wall house.

三、补充说明

优缺点分析

  • 优点:封装不变的部分,扩展可变的部分;提供公共代码,便于维护;行为由父类控制,子类实现;
  • 缺点:略。

适用场景

多个子类有共同的方法,且逻辑相同,但具体的逻辑内容不同;

注意事项

模板方法要加上final。

四、参考

  1. https://www.runoob.com/design-pattern/template-pattern.html
  2. https://refactoring.guru/design-patterns/template-method/java/example
  3. https://www.tutorialspoint.com/design_pattern/template_pattern.htm

设计模式系列博文导航

一、创建型 - 5种

原型模式(Prototype Pattern)
抽象工厂模式(Abstract Factory Pattern)
建造者模式(Builder Pattern)
工厂模式(Factory Pattern)
单例模式(Singleton Pattern)

助记语:原抽建工单

二、结构型 - 8种

享元模式(Flyweight Pattern)
代理模式(Proxy Pattern)
适配器模式(Adapter Pattern)
外观模式(Facade Pattern)

过滤器模式(Filter/Criteria Pattern)
桥接模式(Bridge Pattern)
组合模式(Composite Pattern)
装饰器模式(Decorator Pattern)

助记语:想呆室外,过桥组装

三、行为型 - 11种

责任链模式(Chain of Responsibility Pattern)
命令模式(Command Pattern)
解释器模式(Interpreter Pattern)
中介者模式(Mediator Pattern)
迭代器模式(Iterator Pattern)

观察者模式(Observer Pattern)
策略模式(Strategy Pattern)
状态模式(State Pattern)

备忘录模式(Memento Pattern)
模板方法模式(Template Pattern)
访问者模式(Visitor Pattern)

助记语:责令解中谍,观测状被模仿

你可能感兴趣的:(软件设计,设计模式,模板方法模式,uml)