设计模式之模版设计模式

在我们开发中可能会遇到如下问题,例如我们在写代码的时候计算系统的执行时间,现在需求一:计算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毫秒
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值