装饰者模式(Decorator)

装饰者模式(Decorator)

定义:
指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式。
结构
装饰(Decorator)模式中的角色:
抽象构件(Component)角色 :定义一个抽象接口以规范准备接收附加责任的对象。
具体构件(Concrete Component)角色 :实现抽象构件,通过装饰角色为其添加一些职责。
抽象装饰(Decorator)角色 : 继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
具体装饰(ConcreteDecorator)角色 :实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。
案例
我们使用装饰者模式对快餐店案例进行改进,体会装饰者模式的精髓。
在这里插入图片描述

//快餐接口
public abstract class FastFood {
    private float price;
    private String desc;public FastFood() {
    }public FastFood(float price, String desc) {
        this.price = price;
        this.desc = desc;
    }public void setPrice(float price) {
        this.price = price;
    }public float getPrice() {
        return price;
    }public String getDesc() {
        return desc;
    }public void setDesc(String desc) {
        this.desc = desc;
    }public abstract float cost();  //获取价格
}//炒饭
public class FriedRice extends FastFood {public FriedRice() {
        super(10, "炒饭");
    }public float cost() {
        return getPrice();
    }
}//炒面
public class FriedNoodles extends FastFood {public FriedNoodles() {
        super(12, "炒面");
    }public float cost() {
        return getPrice();
    }
}//配料类
public abstract class Garnish extends FastFood {private FastFood fastFood;public FastFood getFastFood() {
        return fastFood;
    }public void setFastFood(FastFood fastFood) {
        this.fastFood = fastFood;
    }public Garnish(FastFood fastFood, float price, String desc) {
        super(price,desc);
        this.fastFood = fastFood;
    }
}//鸡蛋配料
public class Egg extends Garnish {public Egg(FastFood fastFood) {
        super(fastFood,1,"鸡蛋");
    }public float cost() {
        return getPrice() + getFastFood().getPrice();
    }@Override
    public String getDesc() {
        return super.getDesc() + getFastFood().getDesc();
    }
}//培根配料
public class Bacon extends Garnish {public Bacon(FastFood fastFood) {super(fastFood,2,"培根");
    }@Override
    public float cost() {
        return getPrice() + getFastFood().getPrice();
    }@Override
    public String getDesc() {
        return super.getDesc() + getFastFood().getDesc();
    }
}//测试类
public class Client {
    public static void main(String[] args) {
        //点一份炒饭
        FastFood food = new FriedRice();
        //花费的价格
        System.out.println(food.getDesc() + " " + food.cost() + "元");System.out.println("========");
        //点一份加鸡蛋的炒饭
        FastFood food1 = new FriedRice();
​
        food1 = new Egg(food1);
        //花费的价格
        System.out.println(food1.getDesc() + " " + food1.cost() + "元");System.out.println("========");
        //点一份加培根的炒面
        FastFood food2 = new FriedNoodles();
        food2 = new Bacon(food2);
        //花费的价格
        System.out.println(food2.getDesc() + " " + food2.cost() + "元");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hello蜗牛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值