博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
策略模式
阅读量:6875 次
发布时间:2019-06-26

本文共 975 字,大约阅读时间需要 3 分钟。

封装具体的算法。复制代码
//算法接口   public interface CompressionStrategy {    public OutputStream compress(OutputStream data) throws IOException;} 复制代码
//算法的具体实现public class GzipCompressionStrategy implements CompressionStrategy {    @Override    public OutputStream compress(OutputStream data) throws IOException{        return new GZIPOutputStream(data);    }}复制代码
//策略对象    public class Compressor {    private final CompressionStrategy strategy;    public Compressor(CompressionStrategy strategy){        this.strategy=strategy;    }    public void compress(Path infile,File outfile) throws IOException {        try (OutputStream out = new FileOutputStream(outfile)) {            Files.copy(infile,strategy.compress(out));        }    }  }复制代码
//使用   // old way       Compressor gzip=new Compressor(new GzipCompressionStrategy());       gzip.compress(infile,outfile);       //lambda way       Compressor zip=new Compressor(ZipOutputStream::new);       zip.compress(infile,outfile);复制代码

转载地址:http://eimfl.baihongyu.com/

你可能感兴趣的文章
实战Nginx与PHP(FastCGI)的安装、配置与优化
查看>>
列表去除重复的值
查看>>
CCNP学习之路之VLAN Hopping
查看>>
CentOS6.4内核升级, 2.6.*版本升级 Kernel 3.10.*
查看>>
8.27(文件权限管理 正则表达式)
查看>>
用 zabbix 监测 snmptrap 的主动告警功能
查看>>
HDU1717 小数化分数2
查看>>
delphi 导入excel
查看>>
Linux下 FTP 常见错误 500 530等错误解决方法
查看>>
oracle asm
查看>>
VC基于单文档opengl框架
查看>>
MySQL-proxy 读写分离
查看>>
win7 下面操作memcache
查看>>
BZOJ1037:[ZJOI2008]生日聚会Party(DP)
查看>>
openSUSE13.2安装ruby和rails
查看>>
python 高级函数
查看>>
F.Cards with Numbers
查看>>
Learn Python 004: string slicing
查看>>
简单入门Buffer
查看>>
【HDU】6110 路径交(2017百度之星) 线段树+RMQ-LCA+树链的交
查看>>