【设计模式实践系列】装饰者模式应用订单多重优惠计算

装饰者模式

装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

意图:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。

主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀。

何时使用:在不想增加很多子类的情况下扩展类。

优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。

缺点:多层装饰比较复杂。

注意事项:可代替继承。

模式结构
抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口。
具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。

示例
在这里插入图片描述

根据不同类型的订单装饰对应的优惠金额

OrderDiscountComponent (抽象构件(Component)角色)

public interface OrderDiscountComponent {
    double discountPrice();
}

CommonOrder (具体构件(Concrete Component)角色)

初始订单无优惠,在此基础上准备添加各种其他优惠

public class CommonOrder implements OrderDiscountComponent {

    private double d;

    public CommonOrder(double d) {
        this.d = d;
    }

    @Override
    public double discountPrice() {
        return d;
    }
}

OrderDecorator 装饰(Decorator)角色

public abstract class OrderDecorator implements OrderDiscountComponent {

    protected OrderDiscountComponent orderDiscountComponent;

    public OrderDecorator(OrderDiscountComponent orderDiscountComponent) {
        this.orderDiscountComponent = orderDiscountComponent;
    }

    @Override
    public double discountPrice() {
        return orderDiscountComponent.discountPrice();
    }
}

CouponOrderDecorator (具体装饰(Concrete Decorator)角色)

public class CouponOrderDecorator extends OrderDecorator {
    public CouponOrderDecorator(OrderDiscountComponent orderDiscountComponent) {
        super(orderDiscountComponent);
    }

    @Override
    public double discountPrice() {
        double d = super.discountPrice();
        System.out.println("使用优惠券优惠1元!");
        return d - 1;
    }
}

VIPOrderDecorator(具体装饰(Concrete Decorator)角色)

public class VIPOrderDecorator extends OrderDecorator {
    public VIPOrderDecorator(OrderDiscountComponent orderDiscountComponent) {
        super(orderDiscountComponent);
    }

    @Override
    public double discountPrice() {
        double d = super.discountPrice();
        System.out.println("VIP订单,优惠1元!");
        return d - 1;
    }
}
public class Test {
    public static void main(String[] args) {
        System.out.println("优惠券 订单");
        OrderDecorator coupon = new CouponOrderDecorator(new CommonOrder(10));
        System.out.println("最终订单金额:" + coupon.discountPrice());

        System.out.println("===========================================");

        System.out.println("VIP + 优惠券 订单");
        OrderDecorator vip = new VIPOrderDecorator(new CouponOrderDecorator(new CommonOrder(10)));
        System.out.println("最终订单金额:" + vip.discountPrice());
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码拉松

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

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

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

打赏作者

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

抵扣说明:

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

余额充值