模板方法模式就是定义一个算法的骨架,而将具体的算法延迟到子类去实现。
优点:使用模板方法模式时,在定义算法骨架的同时,可以很灵活的实现具体的算法,满足用户灵活多变的需求。
缺点:如果算法骨架修改的话,则需要修改抽象类。
首先定义一个类,该类的作用是计算一段代码的运行时间:
public class GetTime {
public long getTime() {
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
return end - start;
}
}
测试类:
public class Test {
public static void main(String[] args) {
GetTime gt = new GetTime();
System.out.println(gt.getTime() + "毫秒");
}
}
前面的代码是计算for循环的执行时间,如果要计算复制一段视频的时间,需要修改代码如下:
public class GetTime {
public long getTime() throws IOException {
long start = System.currentTimeMillis();
/*for (int i = 0; i < 10000; i++) {
System.out.println(i);
}*/
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/test.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.avi"))) {
byte[] buffer = new byte[bis.available()];
int len = -1;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (Exception e) {
}
long end = System.currentTimeMillis();
return end - start;
}
}
如果要计算其他操作执行的时间,又要修改代码,这违背了开闭原则,不利于扩展。于是将变化的代码提取出来,放到子类,父类只定义一个抽象的方法,子类继承父类时,重写父类的抽象方法,将变化的代码放到子类中去:
public abstract class GetTime {
public long getTime() throws IOException {
long start = System.currentTimeMillis();
/*for (int i = 0; i < 10000; i++) {
System.out.println(i);
}*/
/*try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/test.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.avi"))) {
byte[] buffer = new byte[bis.available()];
int len = -1;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (Exception e) {
}*/
code();
long end = System.currentTimeMillis();
return end - start;
}
protected abstract void code();
}
public class GetTimeDemo extends GetTime {
@Override
public void code() {
for (int i = 0; i < 10000; i++) {
System.out.println(i);
}
}
}
public class Test {
public static void main(String[] args) throws IOException {
GetTimeDemo gt = new GetTimeDemo();
System.out.println(gt.getTime() + "毫秒");
}
}
如果要计算其他操作执行的时间,只需要编写一个类,继承GetTime,将需要计算执行时间的代码放到相应子类中去即可。