模版(Template)设计模式


模版(Template)设计模式

: 模版设计模式概述
* 模版方法模式就是定义一个算法的骨架,而将具体的算法延迟到子类中来实现
优点和缺点
* a:优点
* 使用模版方法模式,在定义算法骨架的同时,可以很灵活的实现具体的算法,满足用户灵活多变的需求
* b:缺点
* 如果算法骨架有修改的话,则需要修改抽象类


eg


public class a {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*long start = System.currentTimeMillis();
		for(int i = 0; i < 1000000; i++) {
			System.out.println("x");
		}
		long end = System.currentTimeMillis();
		System.out.println(end - start);*/
		Demo d = new Demo();
		System.out.println(d.getTime());
	}

}
//定义计算程序运行时间的抽象骨架,把具体的程序抽取到抽象类code()中实现
abstract class GetTime {
	public final long getTime() {
		long start = System.currentTimeMillis();
		code();
		long end = System.currentTimeMillis();
		return end - start;
	}

	public abstract void code();
}

class Demo extends GetTime {

	@Override
	public void code() {
		int i = 0;
		while(i < 1000) {
			System.out.println("x");
			i++;
		}
	}
}


猜你喜欢

转载自blog.csdn.net/uotail/article/details/72583022