Builder pattern
建造者模式属于Creational design patterns
目的
- 把一个复杂对象的构造跟他的表示分离出来,因而相同的构建过程可以创建不同的表示。
- 解析一个表示,创建一个目标。
实现
假如考虑我们在快餐店有一个商业场景,需要一个典型的meal ,这个meal需要汉堡和冷饮,汉堡可以是蔬菜汉堡或者鸡肉汉堡,用包装纸打包,冷饮或者是可口可乐,或者是百事可乐用瓶子打包。
我们将创建一个 Item 接口表示汉堡和冷饮,并且创建直接类实现item接口和Packing接口因此汉堡将被包装袋打包,冷饮将被瓶子打包。
然后创建Meal类有ArrayList的Item 并且创建一个MealBuidler通过结合Item用来创建不同类型的Meal对象.
Step1
创建一个Item接口表示食物和打包
Item.java
public interface Item {
public String name();
public Packing packing();
public float price();
}
Packing.java
public interface Packing {
public String pack();
}
Step2
创建直接类实现Packing接口
Wrapper.java
package builder;
public class Wrapper implements Packing{
public String pack() {
return "包装袋";
}
}
Bottle.java
package builder;
public class Bottle implements Packing{
public String pack() {
return "瓶子";
}
}
Step3
创建抽象类实现Item接口 提供默认的功能
Burger.java
package builder;
public abstract class Burger implements Item{
public Packing packing() {
return new Wrapper();
}
public abstract float price();
}
ColorDrink.java
package builder;
public abstract class ColorDrink implements Item{
public Packing packing() {
return new Bottle();
}
public abstract float price();
}
Step4
创建直接类继承 Burger 和 ColdDrink
VegBurger.java
package builder;
public class VegBurger extends Burger{
public String name() {
return "蔬菜汉堡";
}
public float price() {
return 25.0f;
}
}
ChickenBurger.java
package builder;
public class ChickenBurger extends Burger{
public float price() {
return 50.5f;
}
public String name() {
return "鸡肉汉堡";
}
}
Coke.java
package builder;
public class Coke extends ColorDrink{
public String name() {
return "可口可乐";
}
public float price() {
return 30.0f;
}
}
Pepsi.java
package builder;
public class Pepsi extends ColorDrink{
public String name() {
return "百事可乐";
}
public float price() {
return 35.0f;
}
}
Step 5
创建有上面定义的item对象的Meal类
Meal.java
package builder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
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.name());
System.out.print(",打包:"+item.packing().pack());
System.out.print(",价格"+item.price());
}
}
}
step6
创建MealBuilder 类 做为实际负责创建Meal对象的类
MealBuilder.java
package builder;
public class MealBuilder {
/**
* 蔬菜汉堡配可口可乐套餐
* @author litao
* @since 2015-8-20下午3:07:45
* @return
*/
public Meal prepareVegMeal(){
Meal meal=new Meal();
meal.addItem(new VegBurger());
meal.addItem(new Coke());
return meal;
}
/**
* 肉汉堡配百事可乐套餐
* @author litao
* @since 2015-8-20下午3:08:21
* @return
*/
public Meal prepareNonVegMeal(){
Meal meal=new Meal();
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi());
return meal;
}
}
Step7 用MealBuilder演示建造者模式
BuilderPatternDemo.java
package builder;
public class BuilderPatternDemo {
public static void main(String[] args){
MealBuilder mealBuilder=new MealBuilder();
Meal vegMeal=mealBuilder.prepareVegMeal();
System.out.print("蔬菜套餐");
vegMeal.showItems();
System.out.print("蔬菜套餐总花费"+vegMeal.getCost());
Meal nonVegMeal=mealBuilder.prepareNonVegMeal();
System.out.print("\n\n 鸡肉套餐");
nonVegMeal.showItems();
System.out.print("鸡肉套餐总花费"+nonVegMeal.getCost());
}
}
输出结果:
蔬菜套餐食物名称蔬菜汉堡,打包:包装袋,价格25.0食物名称可口可乐,打包:瓶子,价格30.0蔬菜套餐总花费55.0
鸡肉套餐食物名称鸡肉汉堡,打包:包装袋,价格50.5食物名称百事可乐,打包:瓶子,价格35.0鸡肉套餐总花费85.5