在我们开发中可能会遇到如下问题,例如我们在写代码的时候计算系统的执行时间,现在需求一:计算for循环的执行时间。需求二:计算文件复制的执行时间。需求三:计算代码执行的时间。我们如何在不来回改变代码的情况下来实现这个需求呢?现在模版模式就可以实现这个效果。代码如下:
原始的代码:
1、实例类
public abstract class CalcTime {
public long calcTime() {
long startTime = System.currentTimeMillis() ;
method() ;
long endTime = System.currentTimeMillis() ;
return endTime - startTime ;
}
public void forMethod() {
for(int x = 0 ; x < 10000 ; x++){
System.out.println(x);
}
// // 赋值文件
// BufferedInputStream bis = null ;
// BufferedOutputStream bos = null ;
//
// try {
// bis = new BufferedInputStream(new FileInputStream("D:\\a.avi")) ;
// bos = new BufferedOutputStream(new FileOutputStream("E:\\a.avi")) ;
//
// byte[] bytes = new byte[1024] ;
// int len = 0 ;
// while((len = bis.read(bytes)) != -1){
// bos.write(bytes, 0, len) ;
// }
//
// } catch (Exception e) {
// e.printStackTrace() ;
// } finally {
//
// // 释放资源
// if(bis != null){
// try {
// bis.close() ;
// } catch (Exception e2) {
// e2.printStackTrace() ;
// }
// }
//
// if(bos != null) {
// try {
// bos.close() ;
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
//
// }
}
2、测试类
public class CalcTimeDemo {
public static void main(String[] args) {
// 创建对象
CalcTime calcTime = new CalcTime() ;
System.out.println("for循环的执行时间为" + calcTime.calcTime() + "毫秒") ;// for循环的执行时间为566毫秒
System.out.println("复制文件的执行时间为" + calcTime.calcTime() + "毫秒") ;// 复制文件的执行时间为161毫秒
}
}
使用模板模式的改造:
首先我们分析下,每次修改需求的时候都需要在CalcTime 里面改代码,修改method里面的代码:所以我们需要把这个代码抽取出来,写成抽象的让子类去实现。然后再测试了里面之间使用,由于抽象类不能实例化,所以我们需要创建他的实例化对象,有什么需求的话让该类直接继承该类,重写他的抽象方法,然后再测试类里面直接实例化,改造的代码如下:
1、抽象类
/**
* 模板设计模式
*/
public abstract class CalcTime {
public long calcTime() {
long startTime = System.currentTimeMillis() ;
method() ;
long endTime = System.currentTimeMillis() ;
return endTime - startTime ;
}
public abstract void method() ;
}
2、实例类
for循环继承自抽象类,重写method方法:
public class ForCalc extends CalcTime {
@Override
public void method() {
for(int x = 0 ; x < 10000 ; x++){
System.out.println(x);
}
}
}
计算文件复制事件,继承自抽象类,重写抽象方法:
public class CopyFileCalc extends CalcTime {
@Override
public void method() {
// 赋值文件
BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
try {
bis = new BufferedInputStream(new FileInputStream("D:\\a.avi")) ;
bos = new BufferedOutputStream(new FileOutputStream("E:\\a.avi")) ;
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len) ;
}
} catch (Exception e) {
e.printStackTrace() ;
} finally {
// 释放资源
if(bis != null){
try {
bis.close() ;
} catch (Exception e2) {
e2.printStackTrace() ;
}
}
if(bos != null) {
try {
bos.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
测试类:
public class CalcTimeDemo {
public static void main(String[] args) {
ForCalc calc = new ForCalc() ;
System.out.println("for循环的执行时间为" + calc.calcTime() + "毫秒");// for循环的执行时间为377毫秒
CopyFileCalc copyFileCalc = new CopyFileCalc() ;
System.out.println("复制文件的执行时间为" +copyFileCalc.calcTime()+ "毫秒");// 复制文件的执行时间为196毫秒
}
}
模板模式计时案例
1182

被折叠的 条评论
为什么被折叠?



