设计模式——装饰器模式

4651321-ec448b15bd8f72e2.jpg
decorator

阅读原文请访问我的博客 BrightLoong's Blog

一. 概述

装饰器模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器模式比生成子类更为灵活;它允许向一个现有的对象添加新的功能,同时又不改变其结构。

装饰器模式属于结构型模式。

二. UML类图解析

装饰器模式的UML类图如下:

4651321-8ccf295d2b1a29d4.png
decorator
  • Component:接口,定义一个抽象接口,真实对象和装饰对象具有相同的接口,以便动态的添加职责。
  • ConcreteComponent:具体的对象。
  • Decorator:装饰类,继承了Component,从外类来扩展Component类的功能,并且持有一个构建引用,进行请求转发。
  • ConcreteDecorator:具体装饰类,用于给实际对象添加职责。

三. 代码实现

现在考虑这样一个场景,现在有一个煎饼摊,人们去买煎饼(Pancake),有些人要加火腿(Ham)的,有些人要加鸡蛋(Egg)的,有些人要加生菜(Lettuce)的,当然土豪可能有啥全给加上_。用上述的装饰器模式来进行编码。

1. 定义煎饼接口IPancake
package io.github.brightloong.design.decorator;

/**
 * 定义一个煎饼接口
 * Created by BrightLoong on 2018/4/22.
 */
public interface IPancake {
    /**
     * 定义烹饪的操作
     */
    void cook();
}

2. 定义具体的煎饼Pancake
package io.github.brightloong.design.decorator;

/**
 * 具体的煎饼对象,可用其他装饰类进行动态扩展。
 * Created by BrightLoong on 2018/4/22.
 */
public class Pancake implements IPancake{
    public void cook() {
        System.out.println("的煎饼");
    }
}

3. 定义抽象装饰类PancakeDecorator
package io.github.brightloong.design.decorator;

/**
 * 实现接口的抽象装饰类,建议设置成abstract.
 * Created by BrightLoong on 2018/4/22.
 */
public abstract  class PancakeDecorator implements IPancake {

    /***/
    private IPancake pancake;

    public PancakeDecorator(IPancake pancake) {
        this.pancake = pancake;
    }

    public void cook() {
        if (this.pancake != null) {
            pancake.cook();
        }
    }
}

4. 具体装饰类EggDecorator
package io.github.brightloong.design.decorator;

/**
 * 对煎饼加鸡蛋的装饰类,继承PancakeDecorator,覆盖cook操作
 * Created by BrightLoong on 2018/4/22.
 */
public class EggDecorator extends PancakeDecorator {
    public EggDecorator(IPancake pancake) {
        super(pancake);
    }

    /**
     * 覆盖cook方法,加入自身的实现,并且调用父类的cook方法,也就是构造函数中
     * EggDecorator(IPancake pancake),这里传入的pancake的cook操作
     */
    @Override
    public void cook() {
        System.out.println("加了一个鸡蛋,");
        super.cook();
    }
}

5. 具体装饰类HamDecorator
package io.github.brightloong.design.decorator;

/**
 * 对煎饼加火腿的装饰类,继承PancakeDecorator,覆盖cook操作
 * Created by BrightLoong on 2018/4/22.
 */
public class HamDecorator extends PancakeDecorator {
    public HamDecorator(IPancake pancake) {
        super(pancake);
    }

    /**
     * 覆盖cook方法,加入自身的实现,并且调用父类的cook方法,也就是构造函数中
     * EggDecorator(IPancake pancake),这里传入的pancake的cook操作
     */
    @Override
    public void cook() {
        System.out.println("加了一根火腿,");
        super.cook();
    }
}

6. 具体装饰类LettuceDecorator
package io.github.brightloong.design.decorator;

/**
 * 对煎饼加生菜的装饰类,继承PancakeDecorator,覆盖cook操作
 * Created by BrightLoong on 2018/4/22.
 */
public class LettuceDecorator extends PancakeDecorator {
    public LettuceDecorator(IPancake pancake) {
        super(pancake);
    }

    /**
     * 覆盖cook方法,加入自身的实现,并且调用父类的cook方法,也就是构造函数中
     * EggDecorator(IPancake pancake),这里传入的pancake的cook操作
     */
    @Override
    public void cook() {
        System.out.println("加了一颗生菜,");
        super.cook();
    }
}

7. 客户端调用以及结果
package io.github.brightloong.design.decorator;

/**
 * 调用客户端
 * Created by BrightLoong on 2018/4/22.
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("=========我是土豪都给我加上===========");
        IPancake pancake = new Pancake();
        IPancake pancakeWithEgg = new EggDecorator(pancake);
        IPancake pancakeWithEggAndHam = new HamDecorator(pancakeWithEgg);
        IPancake panckeWithEggAndHamAndLettuce = new LettuceDecorator(pancakeWithEggAndHam);
        panckeWithEggAndHamAndLettuce.cook();

        System.out.println("==========我是程序猿,加两个鸡蛋补补==============");
        IPancake pancake2 = new Pancake();
        IPancake pancakeWithEgg2 = new EggDecorator(pancake2);
        IPancake pancakeWithTwoEgg = new EggDecorator(pancakeWithEgg2);
        pancakeWithTwoEgg.cook();
    }
}

输出结果

=========我是土豪都给我加上===========
加了一颗生菜,
加了一根火腿,
加了一个鸡蛋,
的煎饼
==========我是程序猿,加两个鸡蛋补补==============
加了一个鸡蛋,
加了一个鸡蛋,
的煎饼

四. 总结

关于装饰器模式的使用,在我看来主要有一下几点需要注意的

  • 抽象装饰器和具体被装饰的对象实现同一个接口
  • 抽象装饰器里面要持有接口对象,以便请求传递
  • 具体装饰器覆盖抽象装饰器方法并用super进行调用,传递请求
1. 适用场景
  • 扩展一个类的功能。
  • 动态添加功能,动态撤销。
2. 优点
  • 装饰类和被装饰类都只关心自身的核心业务,实现了解耦。
  • 方便动态的扩展功能,且提供了比继承更多的灵活性。
3. 缺点
  • 如果功能扩展过多,势必产生大量的类。
  • 多层装饰比较复杂。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java装饰器设计模式是一种结构型设计模式,用于动态地给一个对象添加额外的功能,而不改变其接口。它通过将对象包装在一个具有相同接口的装饰器类中,来实现对原始对象的增强。装饰器模式的主要优点是可拓展性强,可以在不改变原有对象的情况下,动态地给对象扩展功能,即插即用。同时,装饰器模式也易于维护,当某个功能出现问题时,可以直接找到相关的代码进行修复。通过使用不同的装饰类及其排列组合,可以实现不同的效果。然而,装饰器模式会增加许多子类,从而使得代码变得繁多,增加了程序的复杂性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java设计模式——装饰器模式](https://blog.csdn.net/tang_seven/article/details/128977529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Java设计模式之一:装饰器模式](https://blog.csdn.net/yangyin1998/article/details/131508999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值