欢迎观临
路漫漫其修远兮,吾将上下而求索

面向对象设计之建造者模式

建造者模式使用多个简单的对象一步一步构建成一个复杂的对象。可以将复杂对象的创建过程独立出来,简化逻辑。

直接从代码感受吧

//考虑肯德基点餐,一个典型的套餐可以是一个汉堡和一杯冷饮。汉堡可以是素食汉堡或鸡肉汉堡,它们是包在纸盒中。冷饮可以是可口可乐或百事可乐,它们是装在瓶子中。

//先创建一个表示食物条目和食物包装的接口。
public interface Item {
   public String name();
   public Packing packing();
   public float price();
}
public interface Packing {
   public String pack();
}

//实现包装的实体类。
public class Wrapper implements Packing {
   @Override
   public String pack() {
      return "Wrapper";
   }
}
public class Bottle implements Packing {
   @Override
   public String pack() {
      return "Bottle";
   }
}

//抽象出汉堡类和冷饮类
public abstract class Burger implements Item {
   @Override
   public Packing packing() {
      return new Wrapper();
   }
}
public abstract class ColdDrink implements Item {
    @Override
    public Packing packing() {
       return new Bottle();
    }
}

//实现出汉堡和冷饮的具体类
public class VegBurger extends Burger {
   @Override
   public float price() {
      return 25.0f;
   }
   @Override
   public String name() {
      return "Veg Burger";
   }
}
public class ChickenBurger extends Burger {
   @Override
   public float price() {
      return 50.5f;
   }
   @Override
   public String name() {
      return "Chicken Burger";
   }
}
public class Coke extends ColdDrink {
   @Override
   public float price() {
      return 30.0f;
   }
   @Override
   public String name() {
      return "Coke";
   }
}
public class Pepsi extends ColdDrink {
   @Override
   public float price() {
      return 35.0f;
   }
   @Override
   public String name() {
      return "Pepsi";
   }
}

至此,组成几分套餐的基本成分就齐了,接下来我们再定义出套餐类

public class Meal {
   private List<Item> items = new ArrayList<Item>();

   public void addItem(Item item){
      items.add(item);
   }

   public float getCost(){
      float cost = 0.0f;
      for (Item item : items) {
         cost += item.price();
      }
      return cost;
   }

   public void showItems(){
      for (Item item : items) {
         System.out.print("Item : "+item.name());
         System.out.print(", Packing : "+item.packing().pack());
         System.out.println(", Price : "+item.price());
      }
   }
}

有了套餐元素和套餐类,我们就可以创建一个Builder对象,用于创建各种套餐。

public class MealBuilder {
   public Meal prepareVegMeal (){
      Meal meal = new Meal();
      meal.addItem(new VegBurger());
      meal.addItem(new Coke());
      return meal;
   }

   public Meal prepareNonVegMeal (){
      Meal meal = new Meal();
      meal.addItem(new ChickenBurger());
      meal.addItem(new Pepsi());
      return meal;
   }
}

//测试刚刚完成的MealBuilder
public class BuilderPatternDemo {
   public static void main(String[] args) {
      MealBuilder mealBuilder = new MealBuilder();

      Meal vegMeal = mealBuilder.prepareVegMeal();
      System.out.println("Veg Meal");
      vegMeal.showItems();
      System.out.println("Total Cost: " +vegMeal.getCost());

      Meal nonVegMeal = mealBuilder.prepareNonVegMeal();
      System.out.println("\n\nNon-Veg Meal");
      nonVegMeal.showItems();
      System.out.println("Total Cost: " +nonVegMeal.getCost());
   }
}

有时候可能情况会更复杂一些,我们可能会这样定义MealBuilder:

public abstract class MealBuilder {
    Meal m;
    public void buildFood();
    public void buildDrink();
    public Meal getMeal() {
        return m;
    }
}
public class VegMealBuilder implements MealBuilder {
    @Override
    public void buildFood() {
       m..addItem(new VegBurger());
    }
    @Override
    public void buildDrink() {
       m..addItem(new Coke());
    }
}
public class NonVegMealBuilder implements MealBuilder {
    @Override
    public void buildFood() {
       m..addItem(new ChickenBurger());
    }
    @Override
    public void buildDrink() {
       m..addItem(new Pepsi());
    }
}

为了让各种不同的建造者工作起来,就需要引入监工(Director)角色,通过监工来实现使不同的建造者通过统一的流程创建出不同的产品。

public class MealDirector {
    MealBuilder builder;

    MealDirector(MealBuilder b)  {
        this.builder = b;
    }
    public void setBuilder(MealBuilder b) {
        this.builder = b;
    }

    public Meal buildMeal() {
        this.builder.buildFood();
        this.builder.buildDrink();
        return this.builder.getMeal();
    }
}

public class BuilderPatternDemo {
   public static void main(String[] args) {
        VegMealBuilder b1 = new VegMealBuilder();
        NonVegMealBuilder b2 = new NonVegMealBuilder();

        MealDirector director = new MealDirector(b1);
        Meal vegMeal = director.buildMeal();
        System.out.println("Veg Meal");
        vegMeal.showItems();
        System.out.println("Total Cost: " +vegMeal.getCost());

       director.setBuilder(b2);
        Meal nonVegMeal = director.buildMeal();
        System.out.println("\n\nNon-Veg Meal");
nonVegMeal.showItems();
        System.out.println("Total Cost: " +nonVegMeal.getCost());
    }
}
赞(0) 打赏
未经允许不得转载:云海鹰影博客 » 面向对象设计之建造者模式
分享到: 更多 (0)

欢迎留言 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏