意图
装饰模式(Decorator)是一种结构型设计模式, 允许你通过将对象放入包含行为的特殊封装对象中来为原对象绑定新的行为。
装饰模式结构
部件 (Component) 声明封装器和被封装对象的公用接口。
具体部件 (Concrete Component) 类是被封装对象所属的类。 它定义了基础行为, 但装饰类可以改变这些行为。
基础装饰 (Base Decorator) 类拥有一个指向被封装对象的引用成员变量。 该变量的类型应当被声明为通用部件接口, 这样它就可以引用具体的部件和装饰。 装饰基类会将所有操作委派给被封装的对象。
具体装饰类 (Concrete Decorators) 定义了可动态添加到部件的额外行为。 具体装饰类会重写装饰基类的方法, 并在调用父类方法之前或之后进行额外的行为。
客户端 (Client) 可以使用多层装饰来封装部件, 只要它能使用通用接口与所有对象互动即可。
装饰模式适合应用场景
- 如果你希望在无需修改代码的情况下即可使用对象, 且希望在运行时为对象新增额外的行为, 可以使用装饰模式。
装饰能将业务逻辑组织为层次结构, 你可为各层创建一个装饰, 在运行时将各种不同逻辑组合成对象。 由于这些对象都遵循通用接口, 客户端代码能以相同的方式使用这些对象。
- 如果用继承来扩展对象行为的方案难以实现或者根本不可行, 你可以使用该模式。
许多编程语言使用 final
最终关键字来限制对某个类的进一步扩展。 复用最终类已有行为的唯一方法是使用装饰模式: 用封装器对其进行封装。
装饰模式优缺点
优点:
- 你无需创建新子类即可扩展对象的行为。
- 你可以在运行时添加或删除对象的功能。
- 你可以用多个装饰封装对象来组合几种行为。
- 单一职责原则。 你可以将实现了许多不同行为的一个大类拆分为多个较小的类。
缺点:
- 在封装器栈中删除特定封装器比较困难。
- 实现行为不受装饰栈顺序影响的装饰比较困难。
- 各层的初始化配置代码看上去可能会很糟糕。
与其他模式的关系
-
适配器模式可以对已有对象的接口进行修改, 装饰模式则能在不改变对象接口的前提下强化对象功能。 此外, 装饰还支持递归组合, 适配器则无法实现。
-
责任链模式和装饰模式的类结构非常相似。 两者都依赖递归组合将需要执行的操作传递给一系列对象。 但是, 两者有几点重要的不同之处。
责任链的管理者可以相互独立地执行一切操作, 还可以随时停止传递请求。 另一方面, 各种装饰可以在遵循基本接口的情况下扩展对象的行为。 此外, 装饰无法中断请求的传递。
-
组合模式和装饰的结构图很相似, 因为两者都依赖递归组合来组织无限数量的对象。
装饰类似于组合, 但其只有一个子组件。 此外还有一个明显不同: 装饰为被封装对象添加了额外的职责, 组合仅对其子节点的结果进行了 “求和”。
但是, 模式也可以相互合作: 你可以使用装饰来扩展组合树中特定对象的行为。
-
装饰和代理有着相似的结构, 但是其意图却非常不同。 这两个模式的构建都基于组合原则, 也就是说一个对象应该将部分工作委派给另一个对象。 两者之间的不同之处在于代理通常自行管理其服务对象的生命周期, 而装饰的生成则总是由客户端进行控制。
在 Java 中使用模式
使用示例: 装饰在 Java 代码中可谓是标准配置, 尤其是在与流式加载相关的代码中。
Java 核心程序库中有一些关于装饰的示例:
-
java.io.InputStream
、OutputStream
、Reader
和Writer
的所有代码都有以自身类型的对象作为参数的构造函数。 -
java.util.Collections
;checkedXXX()
、synchronizedXXX()
和unmodifiableXXX()
方法。 -
javax.servlet.http.HttpServletRequestWrapper
和HttpServletResponseWrapper
示例代码
部件 (Component)
public interface DataSource {
void writeData(String data) throws IOException;
String readData() throws IOException;
}
具体部件 (Concrete Component)
public class FileDataSource implements DataSource {
private final String name;
public FileDataSource(String name) {
this.name = name;
}
@Override
public void writeData(String data) throws IOException {
File file = new File(name);
try (OutputStream fos = new FileOutputStream(file)) {
fos.write(data.getBytes(), 0, data.length());
}
}
@Override
public String readData() throws IOException {
char[] buffer;
int len;
File file = new File(name);
try (FileReader reader = new FileReader(file)) {
buffer = new char[(int) file.length()];
len = reader.read(buffer);
}
return new String(buffer, 0, len);
}
}
基础装饰 (Base Decorator)
public class DataSourceDecorator implements DataSource {
private final DataSource wrappee;
DataSourceDecorator(DataSource source) {
this.wrappee = source;
}
@Override
public void writeData(String data) throws IOException {
wrappee.writeData(data);
}
@Override
public String readData() throws IOException {
return wrappee.readData();
}
}
具体装饰类 (Concrete Decorators)
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) throws IOException {
super.writeData(compress(data));
}
@Override
public String readData() throws IOException {
return decompress(super.readData());
}
private String compress(String stringData) throws IOException {
byte[] data = stringData.getBytes();
try(ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
DeflaterOutputStream dos = new DeflaterOutputStream(bout, new Deflater(compLevel))) {
dos.write(data);
return Base64.getEncoder().encodeToString(bout.toByteArray());
}
}
private String decompress(String stringData) throws IOException {
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);
}
return new String(bout.toByteArray());
}
}
}
public class EncryptionDecorator extends DataSourceDecorator {
public EncryptionDecorator(DataSource source) {
super(source);
}
@Override
public void writeData(String data) throws IOException {
super.writeData(encode(data));
}
@Override
public String readData() throws IOException {
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);
}
}
客户端 (Client)
public class Client {
public static void main(String[] args) throws IOException {
String salaryRecords = "Name,Salary\nJohn Smith,150000\nSteven Jobs,912000";
DataSourceDecorator encoded = new CompressionDecorator(
new EncryptionDecorator(
new FileDataSource("OutputDemo.txt")));
encoded.writeData(salaryRecords);
DataSource plain = new FileDataSource("OutputDemo.txt");
System.out.println("- Input ----------------");
System.out.println(salaryRecords);
System.out.println("- Encoded --------------");
System.out.println(plain.readData());
System.out.println("- Decoded --------------");
System.out.println(encoded.readData());
}
}