模板模式(Template)

模板方法模式就是定义一个算法的骨架,而将具体的算法延迟到子类去实现。

​​优点:使用模板方法模式时,在定义算法骨架的同时,可以很灵活的实现具体的算法,满足用户灵活多变的需求。

​缺点:如果算法骨架修改的话,则需要修改抽象类。

首先定义一个类,该类的作用是计算一段代码的运行时间:

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,将需要计算执行时间的代码放到相应子类中去即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值