(I/O流)装饰模式
JAVA I/O流就是使用装饰模式这种情景设计的
装饰模式—ecorator)原理:
装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。装饰模式以对客户透明的方式动态的给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不创造更多子类的情况下,装饰模式把客户端的调用委派到被装饰类。装饰模式的关键在于这种扩展完全是透明的。装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象
装饰模式的角色:
--抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。相当于I/O流的抽象类OutputStream/InputStream
--具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
相当于I/O流的节点流FileOutputStream/FileInputStream
--装饰角色—ecorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口。相当于I/O流的FilterOutputStream/FilterInputStream
--具体装饰角色(Concrete Decotator):负责给构件对象“贴上”附加的责任。相当于I/O流的FilterOutputStream/FilterInputStream的子类(缓冲流:BufferedInputStream/BufferedOutputStream;数据流:DataOutputStream/DataInputStream)
下面使用装饰模式设计:
定义Component这个接口类似于I/O流的抽象类OutputStream
/* * 定义抽象构件角色(Component):给出一个抽象接口, * 以规范准备接收附加责任的对象 ,定义这个接口类似于I/O流的抽象类OutputStream/InputStream */package com.decorator;public interface Component {public void doSomething();}
定义ConcreteComponent这个类相当于I/O流的节点流FileOutputStream/FileInputStream
/* * 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。 */package com.decorator;public class ConcreteComponent implements Component {@Overridepublic void doSomething() {System.out.println("功能A");}}
定义这个Decorator类相当于I/O流的过滤流(FilterOutputStream/FilterInputStream)
/* * 装饰角色—ecorator):持有一个构件(Component)对象的引用, * 并定义一个与抽象构件接口一致的接口。 */package com.decorator;public class Decorator implements Component {private Component component; //持有接口的引用;public Decorator(Component component) {this.component = component;}@Overridepublic void doSomething() {component.doSomething();}}
定义下面两个类ConcteteDecorator,ConcteteDecorator2都继承了父类Decorator,相当于I/O流的(缓冲流:BufferedInputStream/BufferedOutputStream;数据流:DataOutputStream/DataInputStream)继承了过滤流FilterOutputStream/FilterInputStream
/* * 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。 */package com.decorator;public class Decorator implements Component {private Component component; //持有接口的引用;public Decorator(Component component) {this.component = component;}@Overridepublic void doSomething() {component.doSomething();}}/* * 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。 */package com.decorator;public class ConcteteDecorator2 extends Decorator {public ConcteteDecorator2(Component component) {super(component);}@Overridepublic void doSomething() {// TODO Auto-generated method stubsuper.doSomething();this.doAnotherThing();}private void doAnotherThing() {System.out.println("功能C");}}
结果测试
/* * 测试结果 */package com.decorator;public class Client {public static void main(String[] args) {//节点流Component component = new ConcreteComponent();//过滤流Component component2 = new ConcteteDecorator(component);//过滤流Component component3 = new ConcteteDecorator2(component2);component3.doSomething();/* * 以面的几行代码整合起来等同于下面这段精简代码,和I/O流的 DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")))相同 *///Component component5 = new ConcteteDecorator2(new ConcteteDecorator(new ConcreteComponent()));//component5.doSomething();}}