作用:用于动态的扩展已有对象的功能,而不用修改原代码,有别于继承,通过继承来扩展行为时所有子类都会继承到相同的行为,且这种行为是编译时静态决定的,不能动态扩展。
OO原则:
组合优于继承
开放关闭原则:对扩展开放,对修改关闭
Example:
装饰者模式其实就是在需要扩展组件的功能时,抛弃继承的方法。使用装饰者封装新行为,然后将组件作为实例变量封装进装饰者中。一般设计时会有四个部分:抽象组件(或接口)、具体组件、抽象装饰着(或接口)、具体装饰者,抽象装饰者最好实现自抽象组件,因为装饰者与被装饰者必须有相同的类型。
在Java中的I/O实现就是装饰者模式:
上面说了抽象装饰器是为了装饰者与被装饰者类型匹配,这样装饰者装饰后返回的还是原类型,就可以无限多层的装饰被装饰者了。
例如我现在需要扩展InputStream的功能:读到字符后将字符装换成大写
第一步:编写自己的装饰者:
public class UpperCaseInputStream extends FilterInputStream{
protected UpperCaseInputStream(InputStream in) {
super(in);
}
public int read(byte b[], int off, int len) throws IOException {
int result = super.read(b,off,len);
for(int i =0;i<b.length;i++) {
b[i] = (byte) Character.toUpperCase((char) b[i]);
}
return result;
}
}
装饰者将扩展了InputStream的功能,在使用时我们就可以使用包含了新功能的InputStream了:
public class InputTest {
public static void main(String[] args) {
try {
byte[] c = new byte[10];
InputStream inputStream = new UpperCaseInputStream(new FileInputStream("/qwer.txt"));
inputStream.read(c,0,10);
for (byte c1: c) {
System.out.print((char) c1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意该模式与继承的区别:
使用装饰者模式时我们就可以在任何时候通过实现新的装饰者来增加新的行为,但是如果使用继承,当需要新的行为时就需要修改现有的代码。这是组合优于继承、开放关闭的思想,比如上面的UpperCaseInputStream,如果现在有需求说要在将字符全部大写的基础上再将字符全部+1呢?我们不可以直接修改UpperCaseInputStream的read方法,因为可能有其他人调用呢,如果是使用继承来扩展行为的话可能说需要再写一个实现类定义新的read方法,这没有必要代码也不易读。正确的做法是写一个新的装饰器来包装UpperCaseInputStream,如下:
定义一个有+1行为的装饰者:
public class UpperAndIncreaseInputStream extends FilterInputStream{
protected UpperAndIncreaseInputStream(InputStream in) {
super(in);
}
public int read(byte b[], int off, int len) throws IOException {
int result = super.read(b,off,len);
for(int i =0;i<b.length;i++) {
b[i] = (byte)(b[i]+1);
}
return result;
}
}
使用的时候就可以通过两层装饰实现添加+1并大写的行为:
public class InputTest {
public static void main(String[] args) {
try {
byte[] c = new byte[10];
InputStream inputStream = new UpperAndIncreaseInputStream(
new UpperCaseInputStream(new FileInputStream("/qwer.txt")));
inputStream.read(c,0,10);
for (byte c1: c) {
System.out.print((char) c1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}