当前位置:网站首页>行为型模式-模板方法模式
行为型模式-模板方法模式
2022-08-02 09:50:00 【vbirdbest】
行为型模式
行为型模式用于描述程序在运行时复杂的流程控制,即描述多个类或对象之间怎样相互协作共同完成单个对象都无法单独完成的任务,它涉及算法与对象间职责分配。
行为型模式分为类行为模式和对象行为模式,前者采用继承机制来在类间分派行为,后者采用组合或者聚合在对象间分配行为。由于组合关系或者聚合关系比继承关系耦合度第,满足“合成复用原则”,所以对象行为模式比类行为模式具有更大的灵活性。
模板方法模式
去银行办理一些业务一般都需要经过以下4个步骤:1.取号 2.排队 3.办理具体业务 4. 对工作人员评分。每个人都会按照顺序经历这4个步骤,但是每个人可能办理的业务不同。
我们可以将固定的过程放到父类中统一实现,将变化的东西放到子类中去实现。
案例
public abstract class BankService {
// 抽象方法
public abstract void handleBusiness();
// 模板方法
public final void service() {
System.out.println("1.取号");
System.out.println("2.排队");
handleBusiness();
System.out.println("4.评分");
}
}
使用模板方法模式就是把公共的代码抽取到父类中,使得子类只需要变的东西,使得子类的代码非常简洁。
public class GetMoneyBankService extends BankService {
@Override
public void handleBusiness() {
System.out.println("3.办理取钱业务");
}
}
public class PutMoneyBankService extends BankService {
@Override
public void handleBusiness() {
System.out.println("3.办理存钱业务");
}
}
public class Client {
public static void main(String[] args) {
BankService bankService = new PutMoneyBankService();
bankService.service();
bankService = new GetMoneyBankService();
bankService.service();
}
}
模板方法套路
- 模板类是抽象类,使用abstract修饰
- 模板方法使用final修饰,并实现具体的步骤。
- 抽象方法使用abstract。
- 具体类需要继承模板类并实现模板类中的抽象方法。
优缺点
优点:
- 提高代码复用性:将相同部分的代码放在抽象的父类中,而将不同的代码放入不同的子类中。
- 实现了反向控制:通过一个父类调用子类的操作,通过对子类的具体实现扩展不同的行为,实现了反向控制,并符合开闭原则。
缺点:
- 对每个不同的实现都需要定义一个子类,这会导致类的个数增加,系统更加庞大,设计也更加抽象。
- 父类中的抽象方法由子类实现,子类执行的结果会影响父类的结果,这导致一种反向的控制结构,它提高了代码的阅读难度。
JDK源码解析
public abstract class InputStream implements Closeable {
// 抽象方法,子类必须重写
public abstract int read() throws IOException;
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
// 模板方法:定义了算法的框架步骤
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
// 调用当前类的抽象方法
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
}
边栏推荐
- 第十六章 协程
- 链表的实现
- Verilog的随机数系统任务----$random
- Linux system uninstall, install, upgrade, migrate clickHouse database
- Mistakes in Brushing the Questions 1-Implicit Conversion and Loss of Precision
- 食品安全 | 鱼肝油不是鱼油,家有宝宝的注意了
- C语言volatile关键字、内嵌汇编volatile与编译器的爱恨情仇
- AutoJs学习-密码生成器
- LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
- 适配器模式适配出栈和队列及优先级队列
猜你喜欢

带你认识40G单纤双向光模块-QSFP+ BiDi光模块

Facebook's automated data analysis solution saves worry and effort in advertising

Verilog的随机数系统任务----$random

周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...

李航《统计学习方法》笔记之感知机perceptron

【OpenCV】-霍夫变换

leetcode 62. Unique Paths(独特的路径)

Getting Started with SCM from Scratch (1): Summary of Background Knowledge

瑞萨RZ/G2L处理器详细测评

享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
随机推荐
mysql进阶(二十一)删除表数据与数据库四大特性
中国发布丨滴滴因违反网络安全法等被罚80.26亿元!调查细节公布
Facebook自动化数据分析方案,广告投放省心省力
The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化水平柱状图(条形图)、使用orientation参数设置柱状图转置为条形图
使用scrapy 把爬到的数据保存到mysql 防止重复
第十六章 协程
牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
armv7与armv8的区别(v8和w12的区别)
iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?
qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)
全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the stacked bar plot, the lab.pos parameter specifies the position of the numerical label of the bar cha
从零开始入门单片机(一):必会背景知识总结
Using the TCP protocol, will there be no packet loss?
AlterNET Studio用户界面设计功能扩展
Pytorch's LSTM parameters explained
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧