欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 九、结构型(装饰者模式)

九、结构型(装饰者模式)

2024/10/25 23:28:05 来源:https://blog.csdn.net/xiaoqi270620903/article/details/142882207  浏览:    关键词:九、结构型(装饰者模式)

装饰者模式

概念
装饰者模式(Decorator Pattern)是一种结构型设计模式,允许向一个对象动态地添加额外的职责,而不影响其他对象。与继承相比,装饰者模式更加灵活,可以在运行时为对象添加新的功能。


应用场景

  1. 动态地扩展对象的功能:当你需要在运行时动态添加对象功能,而不修改原始类或通过继承增加子类时,可以使用装饰者模式。例如为用户界面组件动态添加滚动条、边框等功能。
  2. 避免类爆炸:如果使用继承来扩展功能会导致大量子类的增加,装饰者模式可以通过对象组合减少子类的数量。
  3. 扩展对象功能的灵活性:你可以根据需要选择装饰对象,以组合不同的功能,达到灵活扩展的目的。

注意点

  • 注意装饰链的顺序:装饰者可以嵌套调用,但装饰顺序会影响最终效果。
  • 装饰者与被装饰者接口一致:确保装饰者和被装饰的对象实现相同的接口,以便可以相互替换。
  • 运行时灵活性:装饰者模式比静态继承更具灵活性,但可能增加系统的复杂性,因为需要动态组合多个装饰对象。

核心要素

  1. Component(抽象构件):定义被装饰对象的接口,装饰者和具体构件都要实现这个接口。
  2. ConcreteComponent(具体构件):实现抽象构件接口,表示被装饰的具体对象。
  3. Decorator(装饰者抽象类):持有一个构件对象的引用,并定义与抽象构件相同的接口,用来装饰具体对象。
  4. ConcreteDecorator(具体装饰者):扩展装饰者,增加额外的功能。

Java代码完整示例

// 抽象构件
interface Component {void operation();
}// 具体构件
class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("具体构件的操作");}
}// 抽象装饰者
abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {component.operation();  // 委托给具体构件}
}// 具体装饰者A
class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {super.operation();addedBehaviorA();}private void addedBehaviorA() {System.out.println("具体装饰者A的额外行为");}
}// 具体装饰者B
class ConcreteDecoratorB extends Decorator {public ConcreteDecoratorB(Component component) {super(component);}@Overridepublic void operation() {super.operation();addedBehaviorB();}private void addedBehaviorB() {System.out.println("具体装饰者B的额外行为");}
}// 客户端
public class Client {public static void main(String[] args) {Component component = new ConcreteComponent();// 使用装饰者A装饰具体构件Component decoratorA = new ConcreteDecoratorA(component);decoratorA.operation();System.out.println("-----");// 使用装饰者B装饰具体构件Component decoratorB = new ConcreteDecoratorB(component);decoratorB.operation();System.out.println("-----");// 使用装饰者A和B嵌套装饰Component decoratorAB = new ConcreteDecoratorA(new ConcreteDecoratorB(component));decoratorAB.operation();}
}

输出结果

具体构件的操作
具体装饰者A的额外行为
-----
具体构件的操作
具体装饰者B的额外行为
-----
具体构件的操作
具体装饰者B的额外行为
具体装饰者A的额外行为

各种变形用法完整示例

  1. 动态添加多个装饰功能
    可以将装饰者模式嵌套使用,动态添加多个装饰功能。

    代码示例

    public class ClientMultipleDecorators {public static void main(String[] args) {Component component = new ConcreteComponent();// 动态添加多个装饰功能Component decoratorA = new ConcreteDecoratorA(component);Component decoratorB = new ConcreteDecoratorB(decoratorA);decoratorB.operation(); // 先执行A的功能,再执行B的功能}
    }
    
  2. 透明装饰者模式
    装饰者和具体组件具有相同接口,客户端无需关心对象是否被装饰。

    代码示例

    // 客户端透明调用
    public class ClientTransparentDecorator {public static void main(String[] args) {Component component = new ConcreteComponent();Component decorator = new ConcreteDecoratorA(new ConcreteDecoratorB(component));// 客户端透明调用,无需关心对象是否被装饰decorator.operation();}
    }
    
  3. 装饰者模式与职责链模式结合
    将装饰者模式与职责链模式结合,可以让多个装饰者顺序处理请求。

    代码示例

    // 抽象构件
    interface Component {void operation();
    }// 具体构件
    class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("具体构件的操作");}
    }// 抽象装饰者
    abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {if (component != null) {component.operation();}}
    }// 具体装饰者A
    class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {super.operation();System.out.println("装饰者A处理");}
    }// 具体装饰者B
    class ConcreteDecoratorB extends Decorator {public ConcreteDecoratorB(Component component) {super(component);}@Overridepublic void operation() {super.operation();System.out.println("装饰者B处理");}
    }// 客户端
    public class ClientDecoratorChain {public static void main(String[] args) {Component component = new ConcreteComponent();Component decoratorA = new ConcreteDecoratorA(component);Component decoratorB = new ConcreteDecoratorB(decoratorA);decoratorB.operation(); // 顺序处理请求}
    }
    
  4. 使用Java I/O类库中的装饰者模式
    Java标准库中的I/O类库广泛使用了装饰者模式。例如BufferedInputStreamFileInputStream之间的关系就是装饰者模式的应用。

    代码示例

    import java.io.*;public class IOExample {public static void main(String[] args) {try {// FileInputStream 是具体构件,BufferedInputStream 是装饰者InputStream inputStream = new BufferedInputStream(new FileInputStream("test.txt"));int data = inputStream.read();while (data != -1) {System.out.print((char) data);data = inputStream.read();}inputStream.close();} catch (IOException e) {e.printStackTrace();}}
    }
    

通过装饰者模式,可以在不修改对象的前提下,动态地给对象添加新的功能,并且支持多层装饰。该模式的灵活性和可扩展性,使得它在许多场景下非常实用。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com