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

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

一、UML类图

在这里插入图片描述
图中,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)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值