网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 软件设计 > 设计模式 >

四、装饰者模式

时间:2026-02-11 21:57

人气:

作者:admin

标签:

导读:一、模式定义 在不改变原有对象的前提下(不改变ConcreteComponent情况下),给原有对象扩展功能(利用ConcreteDecorator扩展功能,在ConcreteDecorator中注入ConcreteComponent)。如下所示: 二、应...

一、模式定义

  在不改变原有对象的前提下(不改变ConcreteComponent情况下),给原有对象扩展功能(利用ConcreteDecorator扩展功能,在ConcreteDecorator中注入ConcreteComponent)。如下所示:
clipboard

二、应用场景

  扩展一个类的功能,或者给一个类添加一些附加职责

三、优点:

①、灵活改变组合;
②、符合开闭原则;

四、装饰者模式的实现方式

4.1、方式1
package decorator;

public class DecoratorDesignPattern {
   public static void main(String[] args) {
      Decorator decoratorA = new ConcreteDecoratorA(new ConcreteComponent());
      decoratorA.operation();

      Decorator decoratorB = new ConcreteDecoratorB(new ConcreteDecoratorA(new ConcreteComponent()));
      decoratorB.operation();
   }
}

interface Component{
   void operation();
}

class ConcreteComponent implements Component{

   @Override
   public void operation() {
      System.out.println("拍照...\n");
   }
}

abstract class Decorator implements Component{
   Component component;

   public Decorator(Component component) {
      this.component = component;
   }
}

class ConcreteDecoratorA extends Decorator{

   public ConcreteDecoratorA(Component component) {
      super(component);
   }

   @Override
   public void operation() {
      System.out.println("添加美颜....");
      component.operation();
   }
}

class ConcreteDecoratorB extends Decorator{
   public ConcreteDecoratorB(Component component) {
      super(component);
   }

   @Override
   public void operation() {
      System.out.println("添加滤镜");
      component.operation();
   }
}

上面代码的运行结果如下:
clipboard

4.2、方式2
package decorator;

public class DecoratorPattern {
   public static void main(String[] args) {
      Circle circle = new Circle();
      RedShapeDecorator redCircle = new RedShapeDecorator(circle);
      Rectangle rectangle = new Rectangle();
      RedShapeDecorator redRectangle = new RedShapeDecorator(rectangle);

      System.out.println("Circle with normal border");
      circle.draw();

      System.out.println("\nCircle of red border");
      redCircle.draw();

      System.out.println("\nRectangle of red border");
      redRectangle.draw();

   }
}


interface Shape {
   void draw();
}

class Circle implements Shape {
   @Override
   public void draw() {
      System.out.println("Shape:Circle");
   }
}

class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Shape:Rectangle");
   }
}


abstract class ShapeDecorator implements Shape {
   public Shape decoratedShape;

   public ShapeDecorator(Shape decoratorShape) {
      this.decoratedShape = decoratorShape;
   }

   @Override
   public void draw() {
      decoratedShape.draw();
   }
}

class RedShapeDecorator extends ShapeDecorator {

   public RedShapeDecorator(Shape decoratorShape) {
      super(decoratorShape);
   }

   @Override
   public void draw() {
      decoratedShape.draw();
      setRedBorder(decoratedShape);

   }

   private void setRedBorder(Shape decoratedShape){
      System.out.println("Border Color: Red");
   }
}

上面代码的运行结果如下:
clipboard

五、装饰者模式的应用

  Java IO 库采用了装饰器模式(Decorator Pattern)和适配器模式(Adapter Pattern)的组合设计模式,其中InputStream是装饰器模式中顶层的抽象类,FilterInputStream是装饰器基类,BufferedInputStream是带有缓冲区的装饰器类,ObjectInputStream是可以读取对象的装饰器类,SequenceInputStream是可以顺序读取多个输入Stream的装饰器类,FileInputStream、ByteArrayInputStream则是被装饰的类。 这些类的UML图,如下所示:
image

上一篇:

下一篇:没有了

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
  • 四、装饰者模式

    四、装饰者模式

    一、模式定义 在不改变原有对象的前提下(不改变ConcreteComponent情况下),给原...
  • 三、适配器模式

    三、适配器模式

    一、模式定义 将一个类的接口转换成使用者希望的另一个接口,Adapter模式使得...
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信