初学设计模式之装饰模式(Decorator)

定义
动态的给一个对象增加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
优点
1、把类中的装饰功能从类中搬移去除,可以简化原有的类。
2、有效的把类的核心职责和装饰功能区分开了。还可以去除相关类中重复的装饰逻辑。

何时使用
1)需要扩展一个类的功能,或给一个类增加附加责任。
2)需要动态的给一个对象增加功能,这些功能可以再动态地撤销。
3)需要增加一些基本功能的排列组合而产生的非常大量的功能,从而使继承变得 不现实。
角色组成
l 抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
l 具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类
l 装饰角色(Decorator):持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口
l 具体装饰角色(ConcreteDecorator):负责给构件对象“贴上”附加的责任

在这里插入图片描述
举例
1、使用场景
咖啡是一种饮料,咖啡的本质是咖啡豆+水磨出来的。咖啡店现在要卖各种口味的咖啡,如果不使用装饰模式,那么在销售系统中,各种不一样的咖啡都要产生一个类,如果有4中咖啡豆,5种口味,那么将要产生至少20个类(不包括混合口味),非常麻烦。使用了装饰模式,只需要11个类即可生产任意口味咖啡(包括混合口味)。
2、UML类图
在这里插入图片描述
3、代码
Component类

/**
 * Component
 */
public interface Coffee {
    //返回商品描述
    String getDescrption();

    //
    double getMoney();
}

ConcreteComponent类

public class BlackCoffee implements Coffee{
    private String descrption="选择了黑咖啡";
    private double money=30;

    @Override
    public String getDescrption() {
        return descrption;
    }

    @Override
    public double getMoney() {
        return money;
    }
}
public class WhiteCoffee implements Coffee{
    private String descrption="选择了白咖啡";
    private double money=30;

    @Override
    public String getDescrption() {
        return descrption;
    }

    @Override
    public double getMoney() {
        return money;
    }
}

Decorator类

public class Decorator implements Coffee{
    @Override
    public String getDescrption() {
        return "我只是装饰器,我什么都不知道";
    }

    @Override
    public double getMoney() {
        return 0;
    }
}

ConcreteDecorator类

public class Milk extends Decorator {
    String descrption="加了牛奶";
    double money=3;

    private Coffee coffee;

    public Milk(Coffee coffee){
        this.coffee=coffee;
    }

    @Override
    public String getDescrption() {
        return coffee.getDescrption()+"\n"+descrption;
    }

    @Override
    public double getMoney() {
        return coffee.getMoney()+money;//加了牛奶多3元
    }
}
public class Sugar extends Decorator {
    String descrption="加了糖";
    double money=2;

    private Coffee coffee;

    public Sugar(Coffee coffee){
        this.coffee=coffee;
    }

    @Override
    public String getDescrption() {
        return coffee.getDescrption()+"\n"+descrption;
    }

    @Override
    public double getMoney() {
        return coffee.getMoney()+money;//加了糖多3元
    }
}

Test类

public class TestMain {
    public static void main(String[] args) {
        Coffee coffee=new BlackCoffee();//选择黑咖啡
        coffee=new Milk(coffee);//加入牛奶
        coffee=new Sugar(coffee);//加入糖
        System.out.println(coffee.getDescrption()+"\n"+coffee.getMoney());
        Coffee coffee2=new WhiteCoffee();//选择了白咖啡
        coffee2=new Sugar(coffee2);//加入糖
        coffee2=new Sugar(coffee2);//加入糖
        System.out.println(coffee2.getDescrption()+"\n"+coffee2.getMoney());
    }
}

输出

选择了黑咖啡
加了牛奶
加了糖
35.0
选择了白咖啡
加了糖
加了糖
34.0

参考

1、链接:https://blog.csdn.net/zhshulin/article/details/38665187
2、《大话设计模式》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值