设计模式之装饰者模式

目录

 

装饰者模式介绍

路边摊事例

路边摊类图

路边摊实现

Food

EggCake

ChineseHamburger

OriginalMaterial

Egg

Tenderloin

HamSausage

ChineseHamburger

Test


装饰者模式介绍

装饰者模式动态的将责任附加到对象身上。想要拓展功能,装饰者提供有别于继承的另一种选择。

 

路边摊事例

在城市中,总有那么一群人,为我们提供自然的美味肉夹馍和鸡蛋灌饼,在这两种食物上,我们可以额外的加鸡蛋、里脊、火腿、鸡胸肉等等,我们假定他们的价格如下:

食材

价格

备注

肉夹馍

5.0元

主产品

鸡蛋灌饼

4.0元

主产品

鸡蛋

1.0元

附加品

里脊

2.0元

附加品

火腿肠

1.5元

附加品

鸡胸肉

1.5元

附加品

路边摊只提供两种主商品(肉夹馍和鸡蛋灌饼),在这之上,我们可以添加其他的辅料,比如鸡蛋灌饼 + 里脊,那这个时候的价格是6.0元。

 

路边摊类图

 

 

路边摊实现

Food

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public abstract class Food {
    abstract String getDescription();

    abstract double cost();

}

 

EggCake

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public class EggCake extends Food {
    @Override
    public String getDescription() {
        return "鸡蛋灌饼";
    }

    @Override
    public double cost() {
        return 4.0D;
    }
}

ChineseHamburger

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public class ChineseHamburger extends Food {
    @Override
    public String getDescription() {
        return "肉夹馍";
    }

    @Override
    public double cost() {
        return 5.0D;
    }
}

 

OriginalMaterial

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public abstract class OriginalMaterial extends Food {
    private Food food;

    public OriginalMaterial(Food food) {
        this.food = food;
    }

    @Override
    public String getDescription() {
        return food.getDescription() + "+" + materialName();
    }

    @Override
    public double cost() {
        return food.cost() + materialCost();
    }

    abstract double materialCost();

    abstract String materialName();
}

 

Egg

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public class Egg extends OriginalMaterial {
    public Egg(Food food) {
        super(food);
    }

    @Override
    public double materialCost() {
        return 1.0D;
    }

    @Override
    public String materialName() {
        return "鸡蛋";
    }
}

 

Tenderloin

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public class Tenderloin extends OriginalMaterial {
    public Tenderloin(Food food) {
        super(food);
    }
    
    @Override
    public double materialCost() {
        return 2.0D;
    }

    @Override
    public String materialName() {
        return "里脊";
    }
}

 

HamSausage

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public class HamSausage extends OriginalMaterial {
    public HamSausage(Food food) {
        super(food);
    }

    @Override
    public double materialCost() {
        return 1.5D;
    }

    @Override
    public String materialName() {
        return "火腿肠";
    }
}

 

ChineseHamburger

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public class ChineseHamburger extends Food {
    @Override
    public String getDescription() {
        return "肉夹馍";
    }

    @Override
    public double cost() {
        return 5.0D;
    }
}

 

Test

package com.lulei.study.design.patterns.decorator;

/**
 * Created by lulei on 2019/4/19.
 */
public class Test {
    public static void main(String[] args) {
        //购买一个单纯的鸡蛋灌饼
        Food eggCake = new EggCake();
        System.out.println(String.format("顾客一购买\"%s\"花费%s元", eggCake.getDescription(), eggCake.cost()));

        Food eggCake1 = new EggCake();
        //鸡蛋灌饼添加一个鸡蛋
        eggCake1 = new Egg(eggCake1);
        //添加一个里脊
        eggCake1 = new Tenderloin(eggCake1);
        System.out.println(String.format("顾客二购买\"%s\"花费%s元", eggCake1.getDescription(), eggCake1.cost()));

        //购买一个肉夹馍
        Food chineseHamburger = new ChineseHamburger();
        //加火腿肠
        chineseHamburger = new HamSausage(chineseHamburger);
        //加鸡蛋
        chineseHamburger = new Egg(chineseHamburger);
        //加鸡胸肉
        chineseHamburger = new ChickenBreast(chineseHamburger);
        //加里脊
        chineseHamburger = new Tenderloin(chineseHamburger);
        System.out.println(String.format("顾客三购买\"%s\"花费%s元", chineseHamburger.getDescription(), chineseHamburger.cost()));
        
        //购买一个肉夹馍
        Food chineseHamburger2 = new ChineseHamburger();
        //加2个鸡蛋
        chineseHamburger2 = new Egg(chineseHamburger2);
        chineseHamburger2 = new Egg(chineseHamburger2);
        System.out.println(String.format("顾客四购买\"%s\"花费%s元", chineseHamburger2.getDescription(), chineseHamburger2.cost()));
    }
}

 

测试结果

顾客一购买"鸡蛋灌饼"花费4.0元
顾客二购买"鸡蛋灌饼+鸡蛋+里脊"花费7.0元
顾客三购买"肉夹馍+火腿肠+鸡蛋+鸡胸肉+里脊"花费11.0元
顾客四购买"肉夹馍+鸡蛋+鸡蛋"花费7.0元

 

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