设计模式(1)-装饰者设计模式

725 阅读1分钟

前言

最近在回顾Retrofit的源码,其中ExecuteCallBack那部分有点绕,封来封去,后来发现这不就是装饰者设计模式吗?

啥是装饰者设计模式,有啥用?

这玩意主要是用来进行功能增强的,说实话有点像代理,但是它代的太多了。只需要记住“皮裤套棉裤必定有缘故”

举例

在砍一刀App购物的时候,大家会有各种打折+满减+券。这就是很典型的装饰着设计模式。

UML图。

  • 爷爷是一个接口有个方法是输出价格
  • 叔辈
    • 叔叔是一个实现类,【输出价格】方法返回200元
    • 爹是个抽象类,有个构造方法传入【接口实现类】,然后在【输出价格】方法里调用传入的【接口实现类】的【输出价格】方法。
  • 孙子辈
    • 继承爹,来做具体的功能增强,比如在返回结果上+100。有的*1.5。而且他们可以相互嵌套。

image.png

具体代码

我想对输出价格的方法做增强,每种增强不一样,而且可以搭在一起,就可以用装设器。

image.png

package com.kent.lib;

public class Hello {




    public static void main(String[] args) {
        ItemCompnent item = new ConcreteItemComponent();
        item = new ShopDiscountDecorator(item);
        System.out.println("ShopDiscountDecorator:"+item.checkoutPrice());
        System.out.println("----------------------");

        item = new FullReductionDecorator(item);
        System.out.println("FullReductionDecorator:"+item.checkoutPrice());
        System.out.println("----------------------");

        item = new CouponDecorator(item);
        System.out.println("CouponDecorator fianl:"+item.checkoutPrice());
        System.out.println("----------------------");

    }


    public interface ItemCompnent{
        public double checkoutPrice();
    }


    public static class ConcreteItemComponent implements ItemCompnent{

        @Override
        public double checkoutPrice() {
            System.out.println(this.getClass().getSimpleName()+" checkoutPrice()");
            return 200.0;
        }
    }



    public static abstract class ItemAbstractDecorator implements ItemCompnent{
    protected  ItemCompnent itemCompnent;

        public ItemAbstractDecorator(ItemCompnent myItem){
            this.itemCompnent = myItem;
        }

        @Override
        public double checkoutPrice() {
            System.out.println(this.getClass().getSimpleName()+"$$$$$parent checkoutPrice()");

            return this.itemCompnent.checkoutPrice();
        }
    }


    public static class ShopDiscountDecorator extends ItemAbstractDecorator{
        public ShopDiscountDecorator(ItemCompnent myItem) {
            super(myItem);
        }

        @Override
        public double checkoutPrice() {
            System.out.println(this.getClass().getSimpleName()+" checkoutPrice()");

            return 0.8 * super.checkoutPrice();
        }
    }

    public static class FullReductionDecorator extends ItemAbstractDecorator{

        public FullReductionDecorator(ItemCompnent myItem) {
            super(myItem);
        }

        @Override
        public double checkoutPrice() {
            System.out.println(this.getClass().getSimpleName()+" checkoutPrice()");

            return super.checkoutPrice() -20;
        }
    }


    public static class CouponDecorator extends ItemAbstractDecorator{

        public CouponDecorator(ItemCompnent myItem) {
            super(myItem);
        }

        @Override
        public double checkoutPrice() {
            System.out.println(this.getClass().getSimpleName()+" checkoutPrice()");
            return super.checkoutPrice() - 50;
        }
    }







}
```输出内容
``` java

ShopDiscountDecorator checkoutPrice()
ShopDiscountDecorator$$$$$parent checkoutPrice()
ConcreteItemComponent checkoutPrice()
ShopDiscountDecorator:160.0
----------------------
FullReductionDecorator checkoutPrice()
FullReductionDecorator$$$$$parent checkoutPrice()
ShopDiscountDecorator checkoutPrice()
ShopDiscountDecorator$$$$$parent checkoutPrice()
ConcreteItemComponent checkoutPrice()
FullReductionDecorator:140.0
----------------------
CouponDecorator checkoutPrice()
CouponDecorator$$$$$parent checkoutPrice()
FullReductionDecorator checkoutPrice()
FullReductionDecorator$$$$$parent checkoutPrice()
ShopDiscountDecorator checkoutPrice()
ShopDiscountDecorator$$$$$parent checkoutPrice()
ConcreteItemComponent checkoutPrice()
CouponDecorator fianl:90.0
----------------------