装饰者设计模式功能_设计模式装饰模式例子

装饰者设计模式功能_设计模式装饰模式例子,第1张

装饰设计模式功能_设计模式装饰模式例子 装饰者模式装饰器模式(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(); }}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/666571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-18
下一篇 2022-04-18

发表评论

登录后才能评论

评论列表(0条)

保存