美文网首页
装饰者模式

装饰者模式

作者: 云芈山人 | 来源:发表于2021-07-24 23:32 被阅读0次

概述

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。
这个模式创建了一个装饰类,用来包装原来的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

  • 意图
    动态的给一个对象添加额外的职责。装饰器模式相比生成子类更为灵活。

  • 主要解决
    一般而言,扩展一个类的经常使用继承方式实现。继承为类引入静态特征,并随扩展功能的增多,子类会膨胀。

  • 何时使用
    扩展类的功能,但不想增加子类数量。

  • 如何解决
    将具体功能职责划分,同时继承装饰者模式。

  • 关键代码

    1. Component充当抽象角色,并不具体实现。
    2. 修饰类引用和继承Component类,具体扩展类重写父类方法。
  • 应用实例
    一个形状可以有不同的颜色。
    孙悟空,作为猴子,拥有72种变化。
    等等

  • 优点
    装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的替代模式,装饰模式可以动态扩展一个类的功能。

  • 缺点
    多层装饰比较复杂。

  • 使用场景

    1. 扩展一个类的功能。
    2. 动态增加功能,撤销功能。

实现

一个形状装饰上不同的颜色,同时又不改变形状类。我们将这个场景作为实例来演示装饰器模式的用法。

  • 思路
    1. 创建一个 Shape 接口和实现了 Shape 接口的实体类。
    2. 创建一个实现了 Shape 接口的抽象装饰类 ShapeDecorator,并把 Shape 对象作为它的实例变量。
    3. RedShapeDecorator 实现了 ShapeDecorator 的实体类。
    4. DecoratorPatternDemo 使用 RedShapeDecorator 来装饰 Shape 对象。


      Shape装饰器模式.png
public interface Shape {
   void draw();
}
public class Rectangle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Shape: Rectangle");
   }
}
public class Circle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Shape: Circle");
   }
}
public abstract class ShapeDecorator implements Shape {
   protected Shape decoratedShape;
 
   public ShapeDecorator(Shape decoratedShape){
      this.decoratedShape = decoratedShape;
   }
 
   public void draw(){
      decoratedShape.draw();
   }  
}
public class RedShapeDecorator extends ShapeDecorator {
 
   public RedShapeDecorator(Shape decoratedShape) {
      super(decoratedShape);     
   }
 
   @Override
   public void draw() {
      decoratedShape.draw();         
      setRedBorder(decoratedShape);
   }
 
   private void setRedBorder(Shape decoratedShape){
      System.out.println("Border Color: Red");
   }
}
public class DecoratorPatternDemo {
   public static void main(String[] args) {
 
      Shape circle = new Circle();
      ShapeDecorator redCircle = new RedShapeDecorator(new Circle());
      ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle());
      //Shape redCircle = new RedShapeDecorator(new Circle());
      //Shape redRectangle = new RedShapeDecorator(new 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();
   }
}

输出结果
Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle of red border
Shape: Rectangle
Border Color: Red

相关文章

  • 如何利用装饰者模式在不改变原有对象的基础上扩展功能

    目录 什么是装饰者模式 普通示例 装饰者模式示例 类图关系 装饰者模式使用场景 装饰者模式优点 装饰者模式缺点 什...

  • 装饰者模式

    装饰者模式 装饰者模式和适配器模式对比 装饰者模式 是一种特别的适配器模式 装饰者与被装饰者都要实现同一个接口,主...

  • java IO 的知识总结

    装饰者模式 因为java的IO是基于装饰者模式设计的,所以要了解掌握IO 必须要先清楚什么事装饰者模式(装饰者模式...

  • 设计模式-装饰者模式

    装饰者模式概念: 装饰者模式又名包装(Wrapper)模式。装饰者模式以对客户端透明的方式扩展对象的功能,是继承关...

  • java - 装饰者模式

    装饰者模式 装饰者模式:动态将责任添加到对象上。如果需要扩展功能,装饰者提供了比继承更有弹性的解决方案。装饰者模式...

  • 设计模式之装饰者模式(Decorator Pattern)

    What: 装饰者模式又名包装(Wrapper)模式。装饰者模式动态地将责任附加到对象身上。若要扩展功能,装饰者提...

  • 装饰者(Decorator)模式

    装饰者(Decorator)模式装饰模式又名包装(Wrapper)模式。装饰模式是继承关系的一个替代方案。装饰模式...

  • 2、装饰者模式

    装饰者模式 一、基本概念 二、结构 三、案例1、装饰者模式案例2、JavaIO中使用装饰者模式 四、总结 一、基本...

  • PHP的设计模式-装饰者模式

    装饰者模式 装饰者模式 装饰者模式类似蛋糕,有草莓味、奶酪等种类,但是它们的核心都是蛋糕。 不断地将对象添加装饰的...

  • 设计模式 | 装饰者模式及典型应用

    前言 本文的主要内容: 介绍装饰者模式 示例 源码分析装饰者模式的典型应用Java I/O 中的装饰者模式spri...

网友评论

      本文标题:装饰者模式

      本文链接:https://www.haomeiwen.com/subject/bjemyltx.html