1. 意图
5. 效果
1) 比静态继承更加灵活
2) 可以在运行时,添加和删除对象功能
3) 避免在层次结构高层的类有太多的特征
4) 有很多小对象。 对了解系统的人来说,很容易对它们进行定制,但是很难学习这些系统,排错也很困难
6. 代码实现
decorators/DataSource.java: 定义了读取和写入操作的通用数据接口
package decorator.decorators;/** * @author GaoMing * @date 2021/7/12 - 16:48 */public interface DataSource { void writeData(String data); String readData();}
decorators/FileDataSource.java: 简单数据读写器
package decorator.decorators;import java.io.*;/** * @author GaoMing * @date 2021/7/12 - 16:49 */public class FileDataSource implements DataSource{ private String name; public FileDataSource(String name) { this.name = name; } @Override public void writeData(String data) { File file = new File(name); try (OutputStream fos = new FileOutputStream(file)) { fos.write(data.getBytes(), 0, data.length()); } catch (IOException ex) { System.out.println(ex.getMessage()); } } @Override public String readData() { char[] buffer = null; File file = new File(name); try (FileReader reader = new FileReader(file)) { buffer = new char[(int) file.length()]; reader.read(buffer); } catch (IOException ex) { System.out.println(ex.getMessage()); } return new String(buffer); }}
decorators/DataSourceDecorator.java: 抽象基础装饰
package decorator.decorators;/** * @author GaoMing * @date 2021/7/12 - 16:51 */public class DataSourceDecorator implements DataSource{ private DataSource wrappee; DataSourceDecorator(DataSource source) { this.wrappee = source; } @Override public void writeData(String data) { wrappee.writeData(data); } @Override public String readData() { return wrappee.readData(); }}
decorators/EncryptionDecorator.java: 加密装饰
package decorator.decorators;import java.util.Base64;/** * @author GaoMing * @date 2021/7/12 - 16:53 */public class EncryptionDecorator extends DataSourceDecorator{ public EncryptionDecorator(DataSource source) { super(source); } @Override public void writeData(String data) { super.writeData(encode(data)); } @Override public String readData() { return decode(super.readData()); } private String encode(String data) { byte[] result = data.getBytes(); for (int i = 0; i < result.length; i++) { result[i] += (byte) 1; } return Base64.getEncoder().encodeToString(result); } private String decode(String data) { byte[] result = Base64.getDecoder().decode(data); for (int i = 0; i < result.length; i++) { result[i] -= (byte) 1; } return new String(result); }}
decorators/CompressionDecorator.java: 压缩装饰
package decorator.decorators;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Base64;import java.util.zip.Deflater;import java.util.zip.DeflaterOutputStream;import java.util.zip.InflaterInputStream;/** * @author GaoMing * @date 2021/7/12 - 16:54 */public class CompressionDecorator extends DataSourceDecorator{ private int compLevel = 6; public CompressionDecorator(DataSource source) { super(source); } public int getCompressionLevel() { return compLevel; } public void setCompressionLevel(int value) { compLevel = value; } @Override public void writeData(String data) { super.writeData(compress(data)); } @Override public String readData() { return decompress(super.readData()); } private String compress(String stringData) { byte[] data = stringData.getBytes(); try { ByteArrayOutputStream bout = new ByteArrayOutputStream(512); DeflaterOutputStream dos = new DeflaterOutputStream(bout, new Deflater(compLevel)); dos.write(data); dos.close(); bout.close(); return Base64.getEncoder().encodeToString(bout.toByteArray()); } catch (IOException ex) { return null; } } private String decompress(String stringData) { byte[] data = Base64.getDecoder().decode(stringData); try { InputStream in = new ByteArrayInputStream(data); InflaterInputStream iin = new InflaterInputStream(in); ByteArrayOutputStream bout = new ByteArrayOutputStream(512); int b; while ((b = iin.read()) != -1) { bout.write(b); } in.close(); iin.close(); bout.close(); return new String(bout.toByteArray()); } catch (IOException ex) { return null; } }}
Demo.java: 客户端代码
package decorator;import decorator.decorators.*;/** * @author GaoMing * @date 2021/7/12 - 17:03 */public class Demo { public static void main(String[] args) { String salaryRecords = "Name,Salary\nJohn Smith,100000\nSteven Jobs,912000"; DataSourceDecorator encoded = new CompressionDecorator( new EncryptionDecorator( new FileDa......原文转载:http://www.shaoqun.com/a/898500.html
跨境电商:https://www.ikjzd.com/
亿恩网:https://www.ikjzd.com/w/1461
reddit:https://www.ikjzd.com/w/180
netporter:https://www.ikjzd.com/w/2132
1.意图 5.效果 1)比静态继承更加灵活 2)可以在运行时,添加和删除对象功能 3)避免在层次结构高层的类有太多的特征 4)有很多小对象。对了解系统的人来说,很容易对它们进行定制,但是很难学习这些系统,排错也很困难 6.代码实现 decorators/DataSource.java:定义了读取和写入操作的通用数据接口packagedecorator.decorators;/***@
cima:https://www.ikjzd.com/w/1372
吉祥邮:https://www.ikjzd.com/w/1565
新兴龙山温泉是真温泉吗?是天然温泉吗?:http://www.30bags.com/a/230853.html
新兴哪里有看梅花的地方?2021新兴元旦赏梅地推荐?:http://www.30bags.com/a/420695.html
新兴南药汤泉门票2021年4月价格:http://www.30bags.com/a/282444.html
新兴南药汤泉在哪里,怎么走?:http://www.30bags.com/a/282443.html
女人都喜欢被㖭哪里 喜欢让人㖭我下面:http://lady.shaoqun.com/a/247875.html
第一次玩交换真实经历 述自己婆给别人玩经过:http://lady.shaoqun.com/a/247017.html
深圳乐高乐园开始建了吗(开工时间+建成时间):http://www.30bags.com/a/526635.html
深圳保利剧院8月演出汇总2021:http://www.30bags.com/a/526636.html
深圳乐高乐园地址在哪:http://www.30bags.com/a/526637.html
招生在高校旁边开酒店的推广方式有争议:http://lady.shaoqun.com/a/436479.html
No comments:
Post a Comment