一、需求分析
解决某类事情的步骤是固定的,此时我们可以为这类事情提供一个模版代码,从而提高效率
二、模式设计:
1.写出该类事情中的一件事情的解决方案(包括模版部分和变化部分)
2.将会发生变化的部分抽取出来成为一个方法,将其描述为抽象方法
3.使用final修饰模版方法,防止被重写
三、代码实现
用一个简单的小例子来说明:计算程序的执行时间
3.1 工具代码
abstract class CalRuntime{
public final void getTime(){
long startTime = System.currentTimeMillis(); //记录开始的时间
code();
long endTime = System.currentTimeMillis(); //记录结束的时间.
System.out.println("运行时间 :"+ (endTime-startTime));
}
public abstract void code();
}
3.2测试代码
class TestCal extends CalRuntime
{
public static void main(String[] args)
{
TestCal testCal = new TestCal();
testCal.getTime();
}
public void code(){
//your code
}
}
http://m.bbyears.com/jiaocheng/87269.html
推荐访问:设计模式有哪些 23种设计模式