JAVA中的模板方法设计模式

/*
 * 需求:获取一段程序的运行时间
 * 原理:获取程序开始和结束的时间并相减即可
 * 
 * 获取时间: 通过System.currentTimeMillis();
 * 当代码完成优化后,就可以解决这类问题
 * 这种方法,模板方法设计模式
 * 
 * 什么是模板方法
 * 在定义功能时,功能的一部分是确定的,一部分是不确定的,而确定的部分在使用不确定的部分
 * 那么这时就将不确定的部分暴露出去,由该类的子类去完成
 * 
 * 
 * */

abstract class GetTime {
	public final void getTime() {
		long start = System.currentTimeMillis();
		runcode();
		long end = System.currentTimeMillis();
		System.out.println("毫秒:" + (end - start));
	}

	public abstract void runcode();
}

class SubTime extends GetTime{
	public void runcode(){
		for (int x = 0; x < 4000; x++) {
			System.out.println(x);
		}
	}
}

public class TemplateDemo {

	public static void main(String args[]) {
		SubTime gt = new SubTime();
		gt.getTime();
	}

}


                                                                                -----------------------By   仔鸡

猜你喜欢

转载自blog.csdn.net/qq_37325788/article/details/80213410