模板设计模式:定义一个算法的骨架,而将具体的算法延迟到子类中实现。
代码
package cn.idcast4;
public abstract class GetTime {
public long GetTime() {
long start = System.currentTimeMillis();
//code语句必须在start下面,相当于这个语句的的代码
code();
long end = System.currentTimeMillis();
return end - start;
}
//模板是抽象的
public abstract void code();
}
package cn.idcast4;
//要实现的东西必须继承模板
public class ForDemo extends GetTime {
@Override
public void code() {
for (int x = 0; x < 10000; x++) {
System.out.println(x);
}
}
}
package cn.idcast4;
public class GetTimeDemo {
public static void main(String[] args) {
GetTime gt = new ForDemo();
System.out.println(gt.GetTime() + "毫秒");
}
}