装饰者模式

场景:去吃黄焖鸡米饭,发现除了黄焖鸡米饭外,还可以额外的加菜些配菜(Garnish )

分析:
黄焖鸡和配菜都是食物(Food),黄焖鸡是主菜,其他的是配菜,配菜就属于装饰者。

装饰者模式:个人愚见就是给主菜中添加一些配菜,添加装饰作用。有一点很重要,就是在不改变类的行为的情况下,给它添加额外的功能。这里涉及到一个原则:开闭原则(对拓展开放,对修改关闭)

当然,通过继承也可以实现,但是继承的耦合性太强,而且不好组合,所以遇到相类似的场景,要考虑装饰者模式哦。不过装饰者有一个弊端,就是需要创建比较多的类。

类图:
装饰者模式_第1张图片


public abstract class Food {
    /**
     * 买食物需要花费
     */
    public abstract float cost();

    /**
     * 描述
     */
    public abstract String getDescribe();
}

public abstract class Garnish extends Food {//配菜---也就是装饰者
    public Food food;
    public Garnish(Food food){
        this.food = food;
    }
}
public class GreenVegettables extends Garnish {

    public GreenVegettables(Food food) {
        super(food);
    }

    @Override
    public float cost() {
        return food.cost()+2.0f;
    }

    @Override
    public String getDescribe() {
        return food.getDescribe()+",青菜";
    }
}
public class Potato extends Garnish {

    public Potato(Food food) {
        super(food);
    }

    @Override
    public float cost() {
        return food.cost()+2.0f;
    }

    @Override
    public String getDescribe() {
        return food.getDescribe()+",土豆";
    }
}
public class Mushroom extends Garnish {
    public Mushroom(Food food) {
        super(food);
    }

    @Override
    public float cost() {
        return food.cost()+3.0f;
    }

    @Override
    public String getDescribe() {
        return food.getDescribe()+",香菇";
    }
}

public class HuangMenJi extends Food {

    private Size size = Size.MIN_SIZE;
    private Hot hot = Hot.MIN;

    public enum Size {
        MAX_SIZE, MIN_SIZE, MIDDLE_SIZE
    }

    public enum Hot {
        MIN, MIDDLE, MAX, SUPER_MAX
    }

    @Override
    public float cost() {
        switch (size) {
            case MAX_SIZE:

                return 22.0f;
            case MIDDLE_SIZE:

                return 19.0f;
            case MIN_SIZE:

                return 16.0f;
        }
        return 0.0f;
    }

    @Override
    public String getDescribe() {
        return "黄焖鸡米饭" + ","+getSize()+","+getHot();
    }

    public void setSize(Size size) {
        this.size = size;
    }

    public void setHot(Hot hot) {
        this.hot = hot;
    }

    private String getSize() {
        switch (size) {
            case MAX_SIZE:

                return "大份";
            case MIDDLE_SIZE:

                return "中份";
            case MIN_SIZE:

                return "小份";
        }
        return null;
    }

    private String getHot() {
        switch (hot) {
            case MIN:
                return "微辣";
            case MIDDLE:
                return "中辣";
            case MAX:
                return "重辣";
            case SUPER_MAX:
                return "变态辣";
        }
        return "微辣";//默认微辣
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        HuangMenJi huangMenJi = new HuangMenJi();
        huangMenJi.setSize(HuangMenJi.Size.MAX_SIZE);
        huangMenJi.setHot(HuangMenJi.Hot.SUPER_MAX);
        Mushroom mushroom = new Mushroom(new GreenVegettables(new Potato(huangMenJi)));
        String describe = mushroom.getDescribe();
        float cost = mushroom.cost();
        Log.e("zhang", "菜单:"+describe+"    需要支付:"+cost+"元");
    }
}

你可能感兴趣的:(设计模式,设计模式)