设计模式之装饰者模式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() {
        //调用父类的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() {
        //调用父类的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();
    }
}

 运行结果:

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易雪寒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值