设计模式之装饰者模式DecoratorPattern(四)

一、概述

装饰者模式(Decorator Pattern)是一种用于动态地给一个对象添加一些额外的职责的设计模式。就增加功能来说,装饰者模式相比生成子类更为灵活。装饰者模式是一种对象结构型模式。

装饰者模式可以在不改变一个对象本身功能的基础上增强其功能,通过采用组合而非继承的方式,实现了在运行时动态地扩展一个对象的功能。装饰者模式提供了一种比继承更加灵活的方式来扩展一个对象的功能。

二、模式结构

装饰者模式包含以下角色:

  • 抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象。
  • 具体构件(ConcreteComponent)角色:实现抽象构件,具体到某一个对象。
  • 装饰(Decorator)角色:持有一个指向抽象构件的引用并继承抽象构件的接口。
  • 具体装饰(ConcreteDecorator)角色:实现装饰角色,负责为构件对象“贴上”附加的责任。

三、代码实例

1、Component接口

package com.xu.demo.decoratorPattern;

// 抽象构件角色
public interface Component {

    void operation();
}

2、ConcreteComponent类

package com.xu.demo.decoratorPattern;

// 具体构件角色
public class ConcreteComponent implements Component {

    @Override
    public void operation() {
        System.out.println("执行具体构件对象的操作");
    }
}

3、 Decorator类

package com.xu.demo.decoratorPattern;

// 抽象装饰者角色
public class Decorator implements Component {

    protected Component component;

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

    @Override
    public void operation() {
        if (component != null) {
            component.operation();
        }
    }
}

4、ConcreteDecoratorA子类

package com.xu.demo.decoratorPattern;

// 具体装饰角色A
public class ConcreteDecoratorA extends Decorator {

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

    @Override
    public void operation() {
        super.operation();
        addedFunctionA();
    }

    public void addedFunctionA() {
        System.out.println("为构件对象添加功能A");
    }
}

5、 ConcreteDecoratorB子类

package com.xu.demo.decoratorPattern;

// 具体装饰角色B
public class ConcreteDecoratorB extends Decorator {

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

    @Override
    public void operation() {
        super.operation();
        addedFunctionB();
    }

    public void addedFunctionB() {
        System.out.println("为构件对象添加功能B");
    }
}

6、DecoratorPattern类

package com.xu.demo.decoratorPattern;

public class DecoratorPattern {

    public static void main(String[] args) {

        Component component = new ConcreteComponent();

        // 使用装饰者A增强功能
        component = new ConcreteDecoratorA(component);

        // 使用装饰者B进一步增强功能
        component = new ConcreteDecoratorB(component);

        /*
         执行操作,会依次调用ConcreteComponent的operation、
         ConcreteDecoratorA的addedFunctionA、
         ConcreteDecoratorB的addedFunctionB
         */
        component.operation();
    }

}

 运行结果:

相关推荐

  1. 设计模式装饰模式

    2024-04-28 06:14:03       38 阅读
  2. 设计模式装饰模式

    2024-04-28 06:14:03       38 阅读
  3. 装饰模式设计模式

    2024-04-28 06:14:03       3 阅读
  4. 装饰设计模式总结

    2024-04-28 06:14:03       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-28 06:14:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-28 06:14:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-28 06:14:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-28 06:14:03       20 阅读

热门阅读

  1. Kafka分区机制

    2024-04-28 06:14:03       9 阅读
  2. [iOS]APP优化

    2024-04-28 06:14:03       9 阅读
  3. C99 linkedlist 容器实现

    2024-04-28 06:14:03       9 阅读
  4. [前端] todoList制作

    2024-04-28 06:14:03       9 阅读
  5. list

    2024-04-28 06:14:03       12 阅读
  6. 【方案解决思路】RPC服务器不可用

    2024-04-28 06:14:03       11 阅读
  7. CocoaPods使用详解

    2024-04-28 06:14:03       9 阅读
  8. docker部署前端项目(三)简易迅速版本

    2024-04-28 06:14:03       14 阅读
  9. cms增加定时更新网站地图

    2024-04-28 06:14:03       12 阅读