模板方法模式
来源:互联网 发布:深圳市网络微课堂 编辑:程序博客网 时间:2024/06/05 19:34
1类图
2定义
定义一个操作中的算法的框架,而将步骤延迟到子类中。使得子类可以不改变一个算法的结构即可重定义算法的某些特定步骤。
3应用场景
1 多个子类有公有的方法,并且逻辑基本相同时。
2 重要,复杂的算法,可以吧核心算法设计为模板方法,周边的相关细节功能则由各个子类实现。
3 重构时,模板方法模式是一个经常使用的模式,吧相同的代码抽取到父类中,然后通过钩子函数约束其行为。
4抽象Computer
public abstract class AbstractComputer { protected void powerOn(){ System.out.println("开机电源"); } protected void checkHardware(){ System.out.println("硬件检查"); } protected void loadOS(){ System.out.println("载入操作系统"); } protected void login(){ System.out.println("正在进入系统"); } public final void startUp(){ System.out.println("关机"); powerOn(); checkHardware(); loadOS(); login(); System.out.println("关机完成!"); }}
5程序员计算机
public class CoderComputer extends AbstractComputer { @Override protected void login() { // TODO Auto-generated method stub System.out.println("输入程序员的用户名和密码"); }}
6军用计算机
public class MilitaryComputer extends AbstractComputer { @Override protected void checkHardware() { // TODO Auto-generated method stub super.checkHardware(); System.out.println("防火墙检查"); } @Override protected void login() { // TODO Auto-generated method stub System.out.println("生物特征密码"); }}
7测试代码
public class test { public static void main(String[] args) { // TODO Auto-generated method stub AbstractComputer comp = new CoderComputer(); comp.startUp(); System.out.println("-----------------------"); comp = new MilitaryComputer(); comp.startUp(); }}
阅读全文
0 0
- 深入浅出模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法设计模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- TemplateMethod模板方法模式
- 模板方法模式
- 模板方法模式
- 模板方法模式
- 利用pip批量更新python库
- Unity Shader 编程中一些需要记住的技术点
- css3:nth-child()伪类选择器
- POJ-1787 Charlie's Change (完全背包+输出方案 入门题)
- epoll源码实现分析[整理]
- 模板方法模式
- sublime Text快捷键(超级全)
- 插入排序-直接插入排序
- 浅谈Android动画(一)
- Hadoop-MapReduce之WordCount的实现
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path的解决方法
- 关于Python操作SQLite数据库
- 关于 DataSnap Rest Api 方法的定义
- Java中的Set集合