实例
我们考虑了一家快餐店的商业案例,其中典型的一餐可能是汉堡和冷饮。汉堡可以是蔬菜汉堡或鸡肉汉堡,并且可以用包装纸包装。冷饮可以是可口可乐或百事可乐,可以装在瓶中。
我们要创建一个项目代表的食品,如汉堡和冷饮和实施具体的类接口项接口和包装占食品和落实具体的类包装接口包装接口汉堡将在包装和冷饮包装将被包装为瓶子。
然后,我们创建一个具有Item的ArrayList的Meal类和一个MealBuilder,以通过组合Item来构建不同类型的Meal对象。BuilderPatternDemo,我们的演示类将使用MealBuilder来构建Meal。
第1步 - 分别创建一个代表食品和包装的接口。 Item.java,Packing.java
public interface Item {
public String name();
public Packing packing();
public float price();
}
public interface Packing {
public String pack();
}
第2步 - 创建实现 Packing 接口的具体类。Wrapper.java , Bottle.java
public class Wrapper implements Packing {
@Override
public String pack() {
return "Wrapper";
}
}
public class Bottle implements Packing {
@Override
public String pack() {
return "Bottle";
}
}
第3步 - 创建实现提供默认功能的item接口的抽象类。 Burger.java , ColdDrink.java
public abstract class Burger implements Item {
@Override
public Packing packing() {
return new Wrapper();
}
@Override
public abstract float price();
}
步骤4 - 创建扩展Burger和ColdDrink类的具体类VegBurger.java , ChickenBurger.java, Coke.java, Pepsi.java
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";
}
}
第5步 - 创建具有上面定义的Item对象的Meal类。Meal.java
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());
}
}
}
第6步 - 创建一个MealBuilder类,实际的构建器类负责创建Meal对象。 MealBuilder.java
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;
}
}
步骤7 - BuiderPatternDemo使用MealBuider演示了生成器模式。BuilderPatternDemo.java
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());
}
}
步骤8 - 验证输出。
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