装饰模式(decorator pattern) 的原始定义是:动态的给一个对象添加一些额外的职责. 就扩展功能而言,装饰器模式提供了一种比使用子类更加灵活的替代方案.
假设现在有有一块蛋糕,如果只有涂上奶油那这个蛋糕就是普通的奶油蛋糕, 这时如果我们添加上一些蓝莓,那这个蛋糕就是蓝莓蛋糕.如果我们再拿一块黑巧克力 然后写上姓名、插上代表年龄的蜡烛, 这就是变成了一块生日蛋糕
在软件设计中,装饰器模式是一种用于替代继承的技术,它通过一种无须定义子类的方式给对象动态的增加职责,使用对象之间的关联关系取代类之间的继承关系.
装饰(Decorator)模式中的角色:
代码如下
/**
* 抽象构件类
**/
public abstract class Component {
//抽象方法
public abstract void operation();
}
/**
* 具体构建类
**/
public class ConcreteComponent extends Component {
@Override
public void operation() {
//基础功能实现(复杂功能通过装饰类进行扩展)
}
}
/**
* 抽象装饰类-装饰者模式的核心
**/
public class Decorator extends Component{
//维持一个对抽象构件对象的引用
private Component component;
//注入一个抽象构件类型的对象
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
//调用原有业务方法(这里并没有真正实施装饰,而是提供了一个统一的接口,将装饰过程交给子类完成)
component.operation();
}
}
/**
* 具体装饰类
**/
public class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Component component) {
super(component);
}
@Override
public void operation() {
super.operation(); //调用原有业务方法
addedBehavior(); //调用新增业务方法
}
//新增业务方法
public void addedBehavior(){
//......
}
}
我们以一个文件读写器程序为例, 演示一下装饰者模式的使用,下面是该程序的UML类图
导入IO工具类
<dependencies>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.6version>
dependency>
dependencies>
1 ) DataLoader
/**
* 抽象的文件读取接口DataLoader
**/
public interface DataLoader {
String read();
void write(String data);
}
2 ) BaseFileDataLoader
/**
* 具体组件,重写读写方法
**/
public class BaseFileDataLoader implements DataLoader {
private String filePath;
public BaseFileDataLoader(String filePath) {
this.filePath = filePath;
}
@Override
public String read() {
try {
String result = FileUtils.readFileToString(new File(filePath), "utf-8");
return result;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public void write(String data) {
try{
FileUtils.writeStringToFile(new File(filePath), data, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
}
}
3 ) DataLoaderDecorator
/**
* 装抽象饰者类
**/
public class DataLoaderDecorator implements DataLoader {
private DataLoader wrapper;
public DataLoaderDecorator(DataLoader wrapper) {
this.wrapper = wrapper;
}
@Override
public String read() {
return wrapper.read();
}
@Override
public void write(String data) {
wrapper.write(data);
}
}
4 ) EncryptionDataDecorator
/**
* 具体装饰者-对文件内容进行加密和解密
**/
public class EncryptionDataDecorator extends DataLoaderDecorator {
public EncryptionDataDecorator(DataLoader wrapper) {
super(wrapper);
}
@Override
public String read() {
return decode(super.read());
}
@Override
public void write(String data) {
super.write(encode(data));
}
//加密操作
private 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;
}
}
//解密
private String decode(String data) {
try {
Base64.Decoder decoder = Base64.getDecoder();
String result = new String(decoder.decode(data), "UTF-8");
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
5 ) 测试
public class TestDecorator {
public static void main(String[] args) {
String info = "name:tom,age:15";
DataLoaderDecorator decorator = new EncryptionDataDecorator(new BaseFileDataLoader("demo.txt"));
decorator.write(info);
String data = decorator.read();
System.out.println(data);
}
}
装饰器模式的优点:
装饰器模式的缺点:
装饰器模式的适用场景
快速动态扩展和撤销一个类的功能场景。 比如,有的场景下对 API 接口的安全性要求较高,那么就可以使用装饰模式对传输的字符串数据进行压缩或加密。如果安全性要求不高,则可以不使用。
不支持继承扩展类的场景。 比如,使用 final 关键字的类,或者系统中存在大量通过继承产生的子类。