设计模式之建造者模式

建造者模式


/**
 * 创建一个水果套餐Meal类
 */
public class FruitMeal {
    private Apple apple;//苹果--价格
    private Banana banana;//香蕉价格
    private Orange orange;    //桔子价格
    private int discount;//折扣价

    private int totalPrice;//套餐总价

    public void setDiscount(int discount) {
        this.discount = discount;
    }

    public void setApple(Apple apple) {
        this.apple = apple;
    }

    public void setBanana(Banana banana) {
        this.banana = banana;
    }

    public void setOrange(Orange orange) {
        this.orange = orange;
    }

    public int cost() {
        return this.totalPrice;
    }

    public void init() {
        if (null != apple) {
            totalPrice += apple.price();
        }
        if (null != orange) {
            totalPrice += orange.price();
        }
        if (null != banana) {
            totalPrice += banana.price();
        }
        if (totalPrice > 0) {
            totalPrice -= discount;
        }
    }

    public void showItems() {
        System.out.println("totalPrice:" + totalPrice);
    }
}

/**
 * 创建一个MealBuilder类,实际的builder类负责创建套餐Meal对象。
 */
public interface Builder { // 也是工厂
    void buildApple(int price); // 设置苹果

    void buildBanana(int price); // 设置香蕉

    void buildOrange(int price); // 设置桔子

    FruitMeal getFruitMeal(); // 返回创建的套餐
}


import com.enjoy.entity.fruit.Apple;
import com.enjoy.entity.fruit.Banana;
import com.enjoy.entity.fruit.Orange;

/**
 * 假期-建造策略实现类
 */
public class HolidayBuilder implements Builder {
    private FruitMeal fruitMeal = new FruitMeal();

    @Override
    public void buildApple(int price) {
        Apple apple = new Apple();
        apple.setPrice(price);
        fruitMeal.setApple(apple);
    }

    @Override
    public void buildBanana(int price) {
        Banana fruit = new Banana();
        fruit.setPrice(price);
        fruitMeal.setBanana(fruit);
    }

    @Override
    public void buildOrange(int price) {
        Orange fruit = new Orange("Peter", 80);
        fruit.setPrice(price);
        fruitMeal.setOrange(fruit);
    }

    @Override
    public FruitMeal getFruitMeal() {
        fruitMeal.setDiscount(15);// 折扣价格对一个套餐来,是固定的
        fruitMeal.init();
        return fruitMeal;
    }
}


import com.enjoy.entity.fruit.Apple;
import com.enjoy.entity.fruit.Banana;
import com.enjoy.entity.fruit.Orange;

/**
 * 老用户-建造策略实现类
 */
public class OldCustomerBuilder implements Builder {
    private FruitMeal fruitMeal = new FruitMeal();

    @Override
    public void buildApple(int price) {
        Apple apple = new Apple();
        apple.setPrice(price);
        fruitMeal.setApple(apple);
    }

    @Override
    public void buildBanana(int price) {
        Banana fruit = new Banana();
        fruit.setPrice(price);
        fruitMeal.setBanana(fruit);
    }

    @Override
    public void buildOrange(int price) {
        Orange fruit = new Orange("Peter", 80);
        fruit.setPrice(price);
        fruitMeal.setOrange(fruit);
    }

    @Override
    public FruitMeal getFruitMeal() {
        fruitMeal.setDiscount(10);
        fruitMeal.init();
        return fruitMeal;
    }
}

/**
 * 监工类
 */
public class FruitMealDirector {//收银台---监工类
    public void constructOldCustomer() {
        Builder builder = new OldCustomerBuilder();// spring注入方法,

        // 以下代码模板,轻易是不变的
        builder.buildApple(120);// 创建苹果设置价格
        builder.buildBanana(80);// 创建香蕉设置香蕉价格
        builder.buildOrange(50);// 创建桔子设置价格

        FruitMeal fruitMeal = builder.getFruitMeal();

        int cost = fruitMeal.cost();
        System.out.println("本套件花费:" + cost);
    }

    public void constructHoliday() {
        Builder builder = new HolidayBuilder(); // spring注入方法,

        // 以下代码模板,轻易是不变的
        builder.buildApple(120);// 创建苹果设置价格
        builder.buildBanana(80);// 创建香蕉设置香蕉价格
        builder.buildOrange(50);// 创建桔子设置价格

        FruitMeal fruitMeal = builder.getFruitMeal();

        int cost = fruitMeal.cost();
        System.out.println("本套件花费:" + cost);
    }

    public static void main(String[] args) {
        new FruitMealDirector().constructOldCustomer();
        new FruitMealDirector().constructHoliday();
    }
}


https://www.nowcoder.com/discuss/331574?type=2

https://www.nowcoder.com/discuss/347653?type=7

https://www.nowcoder.com/discuss/345547?type=7

https://www.nowcoder.com/discuss/347346?type=0&order=0&pos=14&page=1

https://www.nowcoder.com/discuss/347616?type=1
 

发布了47 篇原创文章 · 获赞 159 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览