Design Pattern 简明教程
Design Patterns - Builder Pattern
建造者模式使用简单对象和逐步方法构建复杂对象。此类设计模式属于创建模式,该模式提供创建对象的最佳方法之一。
Builder pattern builds a complex object using simple objects and using a step by step approach. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
建造者类逐步构建最终对象。此建造者独立于其他对象。
A Builder class builds the final object step by step. This builder is independent of other objects.
Implementation
我们考虑了一个快餐店业务案例,其中典型的套餐可以是汉堡和冷饮。汉堡可以是蔬菜汉堡或鸡肉汉堡,将由包装纸包装。冷饮可以是可乐或百事可乐,将装在瓶子里。
We have considered a business case of fast-food restaurant where a typical meal could be a burger and a cold drink. Burger could be either a Veg Burger or Chicken Burger and will be packed by a wrapper. Cold drink could be either a coke or pepsi and will be packed in a bottle.
我们将创建一个表示食品项目的 Item 接口以及实现 Item 接口的具体类,以及一个表示食品项目包装的 Packing 接口以及实现 Packing 接口的具体类,因为汉堡将用包装纸包装,而冷饮将装在瓶子里。
We are going to create an Item interface representing food items such as burgers and cold drinks and concrete classes implementing the Item interface and a Packing interface representing packaging of food items and concrete classes implementing the Packing interface as burger would be packed in wrapper and cold drink would be packed as bottle.
然后,我们创建一个有 Item ArrayList 的 Meal 类和一个 MealBuilder,通过组合 Item 来构建不同类型的 Meal 对象。我们的演示类 BuilderPatternDemo 将使用 MealBuilder 来构建一个 Meal。
We then create a Meal class having ArrayList of Item and a MealBuilder to build different types of Meal objects by combining Item. BuilderPatternDemo, our demo class will use MealBuilder to build a Meal.
Step 1
创建一个表示食品和包装的 Item 接口。
Create an interface Item representing food item and packing.
Item.java
public interface Item {
public String name();
public Packing packing();
public float price();
}
Packing.java
public interface Packing {
public String pack();
}
Step 2
创建实现 Packing 接口的具体类。
Create concrete classes implementing the Packing interface.
Wrapper.java
public class Wrapper implements Packing {
@Override
public String pack() {
return "Wrapper";
}
}
Bottle.java
public class Bottle implements Packing {
@Override
public String pack() {
return "Bottle";
}
}
Step 3
创建实现 Item 接口的抽象类,提供默认功能。
Create abstract classes implementing the item interface providing default functionalities.
Burger.java
public abstract class Burger implements Item {
@Override
public Packing packing() {
return new Wrapper();
}
@Override
public abstract float price();
}
ColdDrink.java
public abstract class ColdDrink implements Item {
@Override
public Packing packing() {
return new Bottle();
}
@Override
public abstract float price();
}
Step 4
创建扩展 Burger 和 ColdDrink 类的具体类
Create concrete classes extending Burger and ColdDrink classes
VegBurger.java
public class VegBurger extends Burger {
@Override
public float price() {
return 25.0f;
}
@Override
public String name() {
return "Veg Burger";
}
}
ChickenBurger.java
public class ChickenBurger extends Burger {
@Override
public float price() {
return 50.5f;
}
@Override
public String name() {
return "Chicken Burger";
}
}
Coke.java
public class Coke extends ColdDrink {
@Override
public float price() {
return 30.0f;
}
@Override
public String name() {
return "Coke";
}
}
Pepsi.java
public class Pepsi extends ColdDrink {
@Override
public float price() {
return 35.0f;
}
@Override
public String name() {
return "Pepsi";
}
}
Step 5
创建包含上述定义的 Item 对象的 Meal 类。
Create a Meal class having Item objects defined above.
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());
}
}
}
Step 6
创建 MealBuilder 类,这是负责创建 Meal 对象的实际构造器类。
Create a MealBuilder class, the actual builder class responsible to create Meal objects.
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;
}
}
Step 7
BuiderPatternDemo 使用 MealBuider 来演示构建器模式。
BuiderPatternDemo uses MealBuider to demonstrate builder pattern.
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());
}
}