模版方法模式
来源:互联网 发布:中国新歌声网络战队 编辑:程序博客网 时间:2024/06/05 04:03
1) 一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现。
2) 各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。最后,用一个调用这些新的操作的模板方法来替换这些不同的代码。
/**
* 定义模版的抽象类
* @author Administrator
*
*/
public abstract class Templete {
//模版方法
public void templeteOperation(){
System.out.println("模版方法----》");
beforeOperation();
operation();
afterOperation();
}
//不变的方法
private void afterOperation() {
System.out.println("this action afterOperation");
}
//不变的方法
private void beforeOperation() {
System.out.println("this action beforeOperation");
}
//需要子类具体实现的
protected abstract void operation();
}
--------------------------------------------------------------------------------------
/**
* 具体的模版A
* @author Administrator
*
*/
public class Templete_A extends Templete {
@Override
protected void operation() {
System.out.println("this action is In Templete_A");
}
}
--------------------------------------------------------------------------------------
/**
* 具体的模版B
* @author Administrator
*
*/
public class Templete_B extends Templete {
@Override
protected void operation() {
System.out.println("this action is In Templete_B");
}
}
--------------------------------------------------------------------------------------
/**
* 测试类
* @author Administrator
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Templete templete = new Templete_A();
templete.templeteOperation();
templete = new Templete_B();
templete.templeteOperation();
}
}
- 模版方法模式
- 模版方法模式
- 七、模版方法模式
- 模版方法设计模式
- java 模版方法模式
- 模版方法模式
- 模版方法模式
- 模版方法模式
- 7.模版方法模式
- 模版方法模式
- java 模版方法模式
- 模版方法设计模式
- 模版方法模式
- java 模版方法模式
- 模版方法模式
- 模版方法模式
- 模版方法模式
- 模版方法模式
- hdu-1052田忌赛马
- Scala详解----------特征、模式匹配
- 设计模式——第一课
- maven插件介绍之tomcat7-maven-plugin
- HDU 4526 拼车记
- 模版方法模式
- 字节流与字符流的读写学习
- 第六章—函数
- POJ3264--Balanced Lineup(线段树模板题)
- poj解题报告——1953
- 解读CSS布局之-水平垂直居中
- 二分法、快速排序、堆排序 java实现
- Oracle 作业设置
- C语言和C++的不同之处和相通之处