当前位置: 首页 > news >正文

13 结构性模式-装饰器模式

1 装饰器模式介绍

在这里插入图片描述
在软件设计中,装饰器模式是一种用于替代继承的技术,它通过一种无须定义子类的方式给对象动态的增加职责,使用对象之间的关联关系取代类之间的继承关系.

2 装饰器模式原理

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//抽象构件类
public abstract class Component{public abstract void operation();
}
//具体构建类
public class ConcreteComponent extends Component {@Overridepublic void operation() {//基础功能实现(复杂功能通过装饰类进行扩展)}
}
/*** 抽象装饰类-装饰者模式的核心**/
public class Decorator extends Component{//维持一个对抽象构件对象的引用private Component component;//通过构造注入一个抽象构件类型的对象public Decorator(Component component) {this.component = component;}public void operation() {//调用原有的业务方法,并没有真正的进行装饰,而是提供了一个统一的接口,将装饰的过程交给子类完成component.operation();}
}
/*** 具体装饰类**/
public class ConcreteDecorator extends Decorator {public ConcreteDecorator(Component component) {super(component);}@Overridepublic void operation() {super.operation(); //调用原有的业务方法add(); //调用新增的方法}//新增业务方法public void add(){//......}
}
3 装饰器模式应用实例

在这里插入图片描述
在这里插入图片描述
导入IO工具类

<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version>
</dependency>
/*** 抽象的文件读取接口**/
public interface DataLoader {String read();void write(String data);
}
import org.apache.commons.io.FileUtils;import java.io.File;
import java.io.IOException;/*** 具体组件: 抽象文件读取接口的实现类**/
public class BaseFileDataLoader implements DataLoader{private String filePath;public BaseFileDataLoader(String filePath) {this.filePath = filePath;}//读public String read() {try {String result = FileUtils.readFileToString(new File(filePath), "utf-8");return result;} catch (IOException e) {e.printStackTrace();}return null;}//写public void write(String data) {try {FileUtils.writeStringToFile(new File(filePath),data,"utf-8");} catch (IOException e) {e.printStackTrace();}}
}
/*** 抽象装饰者类**/
public class DataLoaderDecorator  implements DataLoader{private DataLoader dataLoader;public DataLoaderDecorator(DataLoader dataLoader) {this.dataLoader = dataLoader;}public String read() {return dataLoader.read();}public void write(String data) {dataLoader.write(data);}
}
import java.io.UnsupportedEncodingException;
import java.util.Base64;/*** 具体装饰者类-对文件内容进行加密和解密**/
public class EncryptionDataDecorator extends DataLoaderDecorator {public EncryptionDataDecorator(DataLoader dataLoader) {super(dataLoader);}@Overridepublic String read() {return decode(super.read());}@Overridepublic void write(String data) {super.write(encode(data));}//加密操作public String encode(String data){try {Base64.Encoder encoder = Base64.getEncoder();byte[] bytes = data.getBytes("utf-8");String result = encoder.encodeToString(bytes);return result;} catch (Exception e) {e.printStackTrace();}return null;}//解密操作public String decode(String data){try {Base64.Decoder decode = Base64.getDecoder();String result = new String(decode.decode(data),"utf-8");return result;} catch (Exception e) {e.printStackTrace();}return null;}
}
4测试
public class TestDecorator {public static void main(String[] args) {String info = "name:tom,age:15";DataLoaderDecorator decorator = newEncryptionDataDecorator(new BaseFileDataLoader("demo.txt"));decorator.write(info);String data = decorator.read();System.out.println(data);}
}
5装饰器模式总结

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/207297.html

相关文章:

  • 支持向量机(SVM)
  • Rabbitmq----分布式场景下的应用
  • springboot + redis实现签到与统计功能
  • Redis | 数据结构(02)SDS
  • Linux C语言开发-D7D8运算符
  • redis 配置主从复制,哨兵模式案例
  • Python---练习:使用for循环实现用户名+密码认证
  • react中使用jquery 语法
  • 服务器中了360后缀勒索病毒怎么解决,勒索病毒解密,数据恢复
  • 使用字节流读取文件中的数据的几种方式
  • Android WMS——概述(一)
  • Node编写获取用户信息接口
  • 【从0到1设计一个网关】自研网关的设计要点以及架构设计
  • 论文-分布式-分布式计算|容错-分布式控制下的自稳定系统
  • C#压缩图片的方法
  • 安装 fcitx + 搜狗/谷歌输入法 之后导致 死机,重启后黑屏只有鼠标可以移动
  • Maven项目转为SpringBoot项目
  • C语言之预处理
  • css步骤条
  • [Hive] 常见函数
  • Mac用NTFS文件夹读写NTFS硬盘 NTFS能复制多大的文件
  • 【unity3D】Scroll Rect组件—制作下滑列表
  • 网络扫描与网络监听
  • Codeforces Round 904 (Div. 2) C
  • DBeaver连接数据库报错:Public Key Retrieval is not allowed 的解决方案
  • DeepFace【部署 04】轻量级人脸识别和面部属性分析框架deepface使用Docker部署CPU+GPU两个版本及cuDNN安装
  • 程序生活 - 减肥小记
  • 深度学习_4_实战_直线最优解
  • 《视觉SLAM十四讲》公式推导(三)
  • pnpm、npm、yarn的区别