定義:
???????? 定義一個算法中的骨架,將一些步驟的執(zhí)行延期到子類,其實JAVA中的操象類就是一個Template模式,因些使用得很普遍,很容易理解。如:
package com.pdw.pattern;
abstract class Benchmark{
?public abstract void benchmark();
?
?/**
? * 重復執(zhí)行的次數(shù)
? * @param count
? * @return
? */
?public final long repeat(int count){
??long startTime;
??if(count<0){
???return 0;
??}else{
???startTime=System.currentTimeMillis();
???for(int i=0;i<count;i++){
????benchmark();
???}
??}
??long stopTime=System.currentTimeMillis();
??return stopTime-startTime;
??
?}
}
class MethodBenchmark extends Benchmark{
?public void benchmark() {
??for(int i=0;i<200000;i++){
???System.out.println("i="+i);
??}
?}
?
}
public class TemplateImpl {
?/**
? * @param args
? */
?public static void main(String[] args) {
??Benchmark operation=new MethodBenchmark();
??long d=operation.repeat(1);
??System.out.println("執(zhí)行一次所需要用的時間:"+d);
?}
}