知乐空间

装饰者设计模式功能

装饰者模式

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,是一种用于代替继承的技术,无需通过继承增加子类就能扩展对象的新功能。使用对象的关联关系代替继承关系,更加灵活,同时避免类型体系的快速膨胀;

主要角色:

  • 组件(Component):组件接口定义了全部组件类和装饰器实现的行为;
  • 组件实现类(ConcreteComponent):实现Component接口,组件实现类就是被装饰器装饰的原始对象,新功能或者附加功能都是通过装饰器添加到该类的对象上的;
  • 装饰器抽象类(Decorator):实现Component接口的抽象类,在其中封装了一个Component 对象, 也就是被装饰的对象;
  • 具体装饰器类(ConcreteDecorator):该实现类要向被装饰的对象添加某些功能;

类图:

一文带你看懂装饰者模式

看具体的例子,就拿去火锅店吃火锅拌蘸料这个来说。

蘸料有很多:

而每个人的口味又不一样。所以组合就非常多。这种情况下如果使用装饰着模式来实现看看。

第一步

首先我们有一个行为拌蘸料(Component)

/**
 * @Author: jimmy
 * @Description:
 *   装饰着模式 -
 *   组件(Component):组件接口定义了全部组件类和装饰器实现的行为;
 */
public interface AllocatSeasoning {
    void allo();
}

接下来,按照上面装饰者的模型进行分析,我们需要2个类来实现上面的行为

第二步

第一个类(ConcreteComponent)可以理解为装拌料的碗,后面的加蘸料都放到这个碗里面

/**
 * @Author: jimmy
 * @Description:
 *  组件实现类(ConcreteComponent):
 *    实现Component接口,组件实现类就是被装饰器装饰的原始对象,
 *    新功能或者附加功能都是通过装饰器添加到该类的对象上的;
 *
 *  可以把这个理解为装拌料的碗
 */
public class AllocDo implements AllocatSeasoning{

    @Override
    public void allo() {
        System.out.println("----- 您好,这边是调料自选区,可以按照您的口味选择自己喜欢的口味 -----");
    }
}

第三步

第二个类,这个类可以理解为抽象

/**
 * @Author: jimmy
 * @Description:
 *  装饰器抽象类(Decorator):
 *        实现Component接口的抽象类,在其中封装了一个Component 对象,
 *     也就是被装饰的对象;
 *
 *    这个对象可以理解为已经准备好了,要去拿蘸料的动作的父级。
 *    AllocatSeasoning可以理解为 蘸料和婉的合体。
 */
public class AllocDecorator implements AllocatSeasoning{

    // 持有对象
    private AllocatSeasoning allocatSeasoning;

    // 构造器
    public AllocDecorator(AllocatSeasoning allocatSeasoning) {
        this.allocatSeasoning = allocatSeasoning;
    }

    // 调用对下个对应的方法
    @Override
    public void allo() {
        allocatSeasoning.allo();
    }
}

第四步

放具体的蘸料(ConcreteDecorator)这里说白了就是 拿着放有蘸料的碗(可以是空碗)来放现在想放的蘸料。我这里只给出了一个具体类,其他的有兴趣可以把代码复制,然后补充下。

/**
 * @Author: jimmy
 * @Description:
 *    具体装饰器类(ConcreteDecorator):该实现类要向被装饰的对象添加某些功能;
 *  这里说白了就是 拿着放有蘸料的碗(可以是空碗)来放现在想放的蘸料
 */
public class GarlicAlloc extends AllocDecorator{

    public GarlicAlloc(AllocatSeasoning allocatSeasoning) {
        super(allocatSeasoning);
    }

    @Override
    public void allo() {
        // 调用原有业务方法
        super.allo();
        // 补充我们这个行为的方法
        addGarlic();
    }

    public void addGarlic() {
        System.out.println("---加蒜---");
    }
}

测试类

public class DecoratorClinet {
    public static void main(String[] args) {
        AllocatSeasoning a = new OilAlloc(new GarlicAlloc(new ChilliAlloc(new AllocDo())));
        a.allo();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 ZLME@ZLME.COM 举报,一经查实,立刻删除。

留言与评论(共有 0 条评论)
验证码: