建造者模式 ( Builder Pattern )(4)

建造者模式 ( Builder Pattern )

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象

建造者模式属于创建型模式,它提供了一种创建对象的最佳方式。

与工厂模式的区别是:建造者模式更加关注与零件装配的顺序

实际情况:
以一家快餐店为例

一个典型的套餐可以是一个汉堡(Burger)和一杯冷饮(Cold drink)

汉堡(Burger)可以是素食汉堡(Veg Burger)或鸡肉汉堡(Chicken Burger),它们是包在纸盒中 冷饮(Cold drink)可以是可口可乐(coke)或百事可乐(pepsi),它们是装在瓶子中

创建一个表示食物条目(比如汉堡和冷饮)的 Item 接口和实现 Item 接口的实体类, 以及一个表示食物包装的 Packing 接口和实现 Packing 接口的实体类,汉堡是包在纸盒中,冷饮是装在瓶子中

然后创建一个 Meal 类,带有 Item 的 ArrayList 和一个通过结合 Item 来创建不同类型的 Meal 对象的 MealBuilder

最后使用 MealBuilder 来创建一个 Meal
创建一个表示食物条目和食物包装的接口

package testDesignPatterns;
public interface Item {
   public String name();
   public Packing packing();
   public float price();    
}
package testDesignPatterns;
public interface Packing {
   public String pack();
}
package testDesignPatterns;
public class Wrapper implements Packing {

   @Override
   public String pack() {
      return "Wrapper";
   }
}
package testDesignPatterns;
public class Bottle implements Packing {

   @Override
   public String pack() {
      return "Bottle";
   }
}

创建实现 Item 接口的抽象类,该类提供了默认的功能

package testDesignPatterns;
public abstract class Burger implements Item {

   @Override
   public Packing packing() {
      return new Wrapper();
   }

   @Override
   public abstract float price();
}

package testDesignPatterns;
public abstract class ColdDrink implements Item {

    @Override
    public Packing packing() {
       return new Bottle();
    }

    @Override
    public abstract float price();
}

创建扩展了 Burger 和 ColdDrink 的实体类

package testDesignPatterns;
public class VegBurger extends Burger {

   @Override
   public float price() {
      return 25.0f;
   }

   @Override
   public String name() {
      return "Veg Burger";
   }
}
package testDesignPatterns;
public class ChickenBurger extends Burger {

   @Override
   public float price() {
      return 50.5f;
   }

   @Override
   public String name() {
      return "Chicken Burger";
   }
}
package testDesignPatterns;
public class Coke extends ColdDrink {

   @Override
   public float price() {
      return 30.0f;
   }

   @Override
   public String name() {
      return "Coke";
   }
}
package testDesignPatterns;
public class Pepsi extends ColdDrink {

   @Override
   public float price() {
      return 35.0f;
   }

   @Override
   public String name() {
      return "Pepsi";
   }
}

创建一个 Meal 类,带有上面定义的 Item 对象

package testDesignPatterns;
import java.util.ArrayList;
import java.util.List;

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());
      }     
   }    
}
package testDesignPatterns;
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;
   }
}
 BuiderPatternDemo 使用 MealBuider 来演示建造者模式(Builder PatternBuilderPatternDemo.java

// author: DDKK.COM 弟弟快看,程序员编程资料站(ddkk.com)
// Copyright © 2015-2065 ddkk.com. All rights reserved.

package com.ddkk.gof;
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());
   }
}

结果:
Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0
Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5

来源:建造者模式

你可能感兴趣的:(设计模式,建造者模式,java,android)