设计模式(八)结构型模式---装饰者模式(decorator)

装饰者模式简介

  • 装饰者模式(Decorator Pattern)是动态的将新对象依附到对象上。相当于对象可以包裹对象本身,然后可以根据递归方式获取想要的信息。
  • 实际使用: JDK中的IO流。

结构

  • 1.抽象构件角色(component):定义一个抽象接口以规范准备接受附加责任的对象。
  • 2.具体构件角色(concrete component):实现抽象构件,通过装饰角色为其添加一些职责。
  • 3.抽象装饰角色(decorator):继承或实现抽象构建,并包含具体构建的实例,可以通过其子类扩展具体构件的功能。
  • 3.具体装饰角色(concrete decorator):实现抽象装饰的相关方法,并给具体构建对象添加附加责任。

UML图

在这里插入图片描述

具体实现

例子:饭店点餐,一份炒饭10元,鸡蛋2元,培根2元,实现该点菜功能。

UML图

在这里插入图片描述

代码实现

  • 抽象构件类
package com.xxliao.pattern.structure.decorator.demo;

/**
 * @author xxliao
 * @description: 抽象构件类-快餐抽象类
 * @date 2024/5/25 12:41
 */
public abstract class FastFood {

    private float price;

    private String desc;

    public FastFood() {
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public FastFood(float price, String desc) {
        this.price = price;
        this.desc = desc;
    }

    /**
     * @description  获取价格方法
     * @author  xxliao
     * @date  2024/5/25 12:42
     */
    public abstract float cost();

}
  • 具体构件类
package com.xxliao.pattern.structure.decorator.demo;

/**
 * @author xxliao
 * @description: 具体构件类 - 炒面类
 * @date 2024/5/25 12:45
 */

public class FriedNoodles extends FastFood{

    public FriedNoodles() {
        super(12,"炒面");
    }

    @Override
    public float cost() {
        return getPrice();
    }
}
package com.xxliao.pattern.structure.decorator.demo;

/**
 * @author xxliao
 * @description: 具体构件类 - 炒饭
 * @date 2024/5/25 12:43
 */

public class FriedRice extends FastFood{

    public FriedRice(){
        super(10,"炒饭");
    }

    @Override
    public float cost() {
        return getPrice();
    }
}
  • 抽象装饰者角色
package com.xxliao.pattern.structure.decorator.demo;

/**
 * @author xxliao
 * @description: 抽象装饰类 - 配料类,继承FastFood,并聚合FastFood类
 * @date 2024/5/25 12:46
 */
public abstract class Garnish extends FastFood{

    private FastFood fastFood;

    public Garnish(FastFood fastFood) {
        this.fastFood = fastFood;
    }

    public Garnish(FastFood fastFood,float price, String desc) {
        super(price, desc);
        this.fastFood = fastFood;
    }

    public void setFastFood(FastFood fastFood) {
        this.fastFood = fastFood;
    }

    public FastFood getFastFood() {
        return fastFood;
    }
}
  • 具体装饰者角色
package com.xxliao.pattern.structure.decorator.demo;

/**
 * @author xxliao
 * @description: 具体装饰类 - 培根配料
 * @date 2024/5/25 12:51
 */
public class Bacon extends Garnish{

    public Bacon(FastFood fastFood) {
        super(fastFood,2,"bacon");
    }

    @Override
    public float cost() {
        return getPrice() + getFastFood().getPrice();
    }

    @Override
    public String getDesc() {
        return super.getDesc() + getFastFood().getDesc();
    }
}
package com.xxliao.pattern.structure.decorator.demo;

/**
 * @author xxliao
 * @description: 具体装饰类 - 鸡蛋类
 * @date 2024/5/25 12:49
 */
public class Egg extends Garnish{

    public Egg(FastFood fastFood) {
        super(fastFood,1,"egg");
    }

    @Override
    public float cost() {
        return getPrice() + getFastFood().getPrice();
    }

    @Override
    public String getDesc() {
        return super.getDesc() + getFastFood().getDesc();
    }
}
  • 测试客户端
package com.xxliao.pattern.structure.decorator.demo;

/**
 * @author xxliao
 * @description: 装饰者模式 测试客户端
 * @date 2024/5/25 12:54
 */
public class Client {
    public static void main(String[] args) {
        // 点一份炒饭
        FastFood friedRice = new FriedRice();

        System.out.println(friedRice.getDesc()+" "+friedRice.cost()+"元");

        // 添加一个鸡蛋
        FastFood eggFriedRice = new Egg(friedRice);
        //
        System.out.println(eggFriedRice.getDesc()+" "+eggFriedRice.cost()+"元");
    }
}
  • 测试结果
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

骑车上酒吧

帮到了您,有闲钱,再打赏哦~

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

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

打赏作者

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

抵扣说明:

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

余额充值