文 / vincentzh
原文连接:http://www.cnblogs.com/vincentzh/p/6057666.html
目录
1、概述
装饰器模式在不改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。通常给一个对象添加功能,要么在类中直接进行修改,要么通过子类继承来扩展对象的功能,又或者使用装饰模式将相应的功能进行任意的组合,使功能的扩展变的具有灵活性和动态性。装饰模式是通过创建一个包装对象,也就是装饰来包裹真实的对象,给真实对象扩展包装对象所具有的功能。
2、目的
装饰器的目的是:通过装饰对象的任意组合,对真实对象进行功能包装,实现对象功能扩展的动态性、简便性、复用性、灵活性。
3、结构组成
装饰模式主要涉及到四个角色:抽象主题角色、具体主题角色、抽象装饰角色、具体装饰角色
抽象主题角色:抽象主题角色提供具体角色和抽象装饰角色共同实现的功能(基本功能方法),具体主题角色和抽象装饰角色都必须去实现抽象主题角色定义的方法;
具体主题角色:具体主题角色就是装饰模式中的真实对象类,实现抽象主题角色的方法(基本功能方法),通过具体装饰角色包装给该角色添加附加功能;
抽象装饰角色:所有装饰器的父类,实现了抽象主题角色的功能(主要是为了实现装饰器功能的复用,即具体的装饰器A可以装饰另外一个具体的装饰器B,因为装饰器类也是一个抽象主题角色),并持有一个抽象主题角色的对象,该对象其实就是被装饰的对象。如果不继承组件接口类,则只能为某个组件添加单一的功能,即装饰器对象不能在装饰其他的装饰器对象。
具体装饰角色:具体的装饰器类,实现具体要向被装饰对象添加的功能。用来装饰具体的组件对象或者另外一个具体的装饰器对象。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAe0AAAEbCAIAAAB4HUuoAAATHElEQVR4nO3d4YGzrBKGYeuyIOqxGpuxmOwP10QUERVhZrivX++3axIchifEuOd0HwCAZl3tAQAAHiHHAUA3chwAdCPHAUA3chwAdCPHAUA3chwAdCPHAUA3chwAdCPHAUA3chwAdCPHAUA3chwAdCPHAUA3chwAdCPHAUA3chwAdCPHAUA3chwAdCPHAUA3crxdXXG1zxiwiaVlR/lcflXtcgJqsFo0qR2tgtSeCkAQ1oNEVnPQ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Pw/eT924GkDw6lfg4tdEjsfJGg2KuZTjLPs4dTme8sY8umXyllL9l2b+mV/EgJPvt73KHn2GWg44G/N2O0GOow2XcpxlH1dxbV9/6UtfXO/fb3//WFfqvOLb0m8n9PeJK3DA1TfmOHIcViS3I8veTo6nV31X36OvEg7ee6eh75xz/oez7nBSck/o8ZSxH4clKe3Isre4H7/k+2HK/5z1/Y/QV86rGzxX79ehu4KSJ/T8u+vUCX2MHIcg77Rjo8teY477t9Pvv6L+/rx3zn8v8z+ErSf7W/ftrf6BDzVXJnTbNg/emIOPv4IchyBX25FlHzlAU44v75X9MH2m6b8K8z+W+Vida+hbi4OfLQ+cq/X/XN47uPfs4/evB7x7lpYOWh26v5t1OyH+iE6+aXl4bYUchyCp7ciyT1j2anJ8udfz9KDVJ5ukCd2+xumLLH0V+HS2efLAZ7hlRn5dsrtzNS4+4VHkOARJakeWfdqyV5PjeExmwWWNBsXIbEelyPF2yCy4rNGgGJntqBQ53g6ZBZc1GhQjsx2VIsfbIbPgskaDYmS2o1LkeDtkFlzWaFCMzHZUihxvh8yCyxoNipHZjkqR4+2QWXBZo0ExMttRKXK8HTILLms0KEZmOypFjrdDZsFljQbFyGxHpcjxdsgsuKzRoBiZ7agUOd4OmQWXNRoU0+EFzGM7ys91hKzRoJjaq8Am5rEd5ec6QtZoUEztVWAT89iO8nMdIWs0AICryHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0I0cBwDdyHEA0O0PSE9pwBLqYrEAAAAASUVORK5CYII=" alt="" />
4、实现
抽象主题角色
package com.cnblogs.vincentzh.decoratormodel;
// 抽象主题角色
// 只定义基本功能方法
public interface Component
{
public void function();
}
具体主题角色
package com.cnblogs.vincentzh.decoratormodel;
// 具体主题角色
public class ConcreteComponent implements Component
{
@Override
public void function()
{
baseFunction();
} public void baseFunction()
{
System.out.println("基础功能");
}
}
抽象装饰角色
package com.cnblogs.vincentzh.decoratormodel;
// 抽象装饰角色
public class Decorator implements Component
{
private Component component; // 持有抽象主题的角色 public Decorator(Component component)
{
this.component = component;
} @Override
public void function()
{
component.function();
}
}
两个具体装饰角色
package com.cnblogs.vincentzh.decoratormodel;
// 具体装饰角色A
public class ConcreteDecoratorA extends Decorator
{
public ConcreteDecoratorA(Component component)
{
super(component);
} @Override
public void function()
{
super.function();
functionA();
} public void functionA()
{
System.out.println("附加功能A");
}
}
package com.cnblogs.vincentzh.decoratormodel;
// 具体装饰角色B
public class ConcreteDecoratorB extends Decorator
{
public ConcreteDecoratorB(Component component)
{
super(component);
} @Override
public void function()
{
super.function();
functionB();
} public void functionB()
{
System.out.println("附加功能B");
}
}
客户端
package com.cnblogs.vincentzh.decoratormodel;
// 客户端测试类
public class Client
{
public static void main(String[] args)
{
Component component = new ConcreteComponent();
Component decoratorA = new ConcreteDecoratorA(component);
Component decoratorB = new ConcreteDecoratorB(decoratorA); decoratorB.function();
// 调用 decoratorB 的 function 方法时,会去先执行父类 Decorator 的 function 方法,而
// Decorator 中持有 Component 的对象引用(所有的被装饰类和装饰类都实现了Component),
// 会去调用传入 Component 引用对象的 function 方法,此处 decoratorB 执行时会先执行
// decoratorA 的方法,而 decoratorA 执行时 又会先去执行 component 的 function 方法,传
// 入的是哪个装饰器的或真实对象的引用就会去先执行引用对象下的方法,这也就是装饰器能够
// 随意组合的缘由所在。
}
}
执行结果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVwAAABlCAIAAACOdsyqAAARyElEQVR4nO2d23Mb1R3H99+JPcNL+iK/dTqThz4wfSiEJnQIU0ZEgYZOQ0NaZhozDBimzMhT3ERAiIESMJN4EpMgYgzBsU3jYqUyxiR2FBtf5FiSLfkiWVpJuzp9WHl17rtarWXF+n3mN56zZ8/98t3frqS1sr46BwYGBmaasustAAMDayhTdr0FYGBgDWXKrrcADAysoUyxmQ4BwF5kd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oAgBj3PQWu44B4G3XnPAWRNhmaYKiCZQcBoDlpXFHgbnJuSm6TOHcT2H8Pr/0fiQPAXsWFLy+Zh9RZNj0VluxnSVFVeQo42JNGRVEUUAUA4OKCp+D4kLufa6lI6lnQtwzbtxIAABDssiiIEkjinYhCpLOtrDzGE0bTaYAHjgBA474osFd+KixxCiyrkGgEW6mlDwIAAAv8dBoAAAIQBQAACEAUAAAggJesAABAAC9ZAQCAAF6yAgAAAbxkBQAAAnjJCgAABM3ykpXKdxrLwHcZAYBPE71kJYL/2iHohfcvAQCXJnrJCi0K8HsoAODRuO9TkOfFD1lPgasO5B0ESAIA8GlcUeBucm5KbpPYuwnaU4DHCgDAo4leshIh36AQgRcqAACP5nnJCqUCQS94CgDAozlessL5SBIkAQD4wEtWAAAggJ9OAwBAAKIAAADB/wHLyfb7Hdl3ewAAAABJRU5ErkJggg==" alt="" />
5、总结
Java 的 I/O 流中就是使用了装饰模式I/O流可以划分为节点流和过滤流。,
节点流:就是直接与输入/输出设备或者文件系统打交道的基本类,如:FileInputStream、FileOutputStream等;
过滤流:过滤过滤,就是对已有的输入/输出流进行相应的附加处理,如缓冲读/写(BufferedInputStream、BufferedOutputStream)等;
InputStream 和 OutputStream 是所有输入/输出流的父类,它就是装饰模式中所谓的抽象主题角色(Component);而属于节点流的类自然就是具体主题角色(ConcreteComponent),它们负责与硬件或文件系统交互,形成最基本的读写操作流;FilterInputStream 和 FilterrOutputStream 则是所有过滤流的父类,同时它也继承自 InputStream 和 OutputStream,对应上去就是抽象装饰角色(Decorator);剩下具体的过滤流就是具体装饰角色(ConcreteDecorator),它对负责基本功能的节点流进行包装添加功能,如:BufferInputStream 添加缓冲读取功能。
在 Hadoop 的编程模型中,MapReduce 用于 HDFS 文件数据读取的两个基本类 FSDataInputStream 和 FSDataOutputStream 也是继承自 Java 的 FilterInputStream 和 FilterOutputStream,那么这样来看,用于 HDFS 文件读/写的原理也是与一般文件读/写的原理并无差别,只是在原有的文件读/写基础上进行了分布式文件读取功能的添加。