参考文档:
1.设计模式 - 可复用面向对象软件的基础
2.http://blog.csdn.net/hguisu/article/details/7518060(设计模式(三)建造者模式Builder(创建型))
3.http://www.cnblogs.com/happyhippy/archive/2010/09/01/1814287.html( Builder模式的误区:将复杂对象的构建进行封装,就是Builder模式了吗?)
关于Builder理论方面的知识,可以从上面三篇获得;
这里实现一个关于Builder的案例。
先看一下关于Builder的结构:
这里实现的案例是:买肯德基,选择对应的套餐,返回相应的食品给用户。
代码实现:
Client.java:
package com.rick.designpattern.builder; /** * Created by MyPC on 2017/6/9. */ public class Client { public void buy(int type) { Builder builder = null; if (1 == type) { builder = new ConcreteBuilder1(); } else if (2 == type) { builder = new ConcreteBuilder2(); } else { builder = new ConcreteBuilder3(); } DirectorCashier directorCashier = new DirectorCashier(); directorCashier.buildFood(builder); builder.getProducts(); } public static void main(String[] args) { Client client = new Client(); client.buy(2); System.out.println("-------------"); client.buy(1); System.out.println("-------------"); client.buy(3); } }
DirectorCashier.java:
package com.rick.designpattern.builder; /** * 指导者:收银员 * <p> * Created by MyPC on 2017/6/9. */ public class DirectorCashier { /** * 收银餐馆员工返回的食物 */ public void buildFood(Builder builder) { builder.buildPart1(); builder.buildPart2(); } }
Builder.java:
package com.rick.designpattern.builder; /** * Created by MyPC on 2017/6/9. */ public abstract class Builder { /** * 创建产品的第一部分 */ public void buildPart1(){ } /** * 创建产品的第二部分 */ public void buildPart2(){ } /** * 返回产品 */ public abstract void getProducts(); }
ConcreteBuilder1.java:
package com.rick.designpattern.builder; /** * 具体建造者类:餐馆员工,返回的套餐是:汉堡两个+饮料一个 * <p> * Created by MyPC on 2017/6/9. */ public class ConcreteBuilder1 extends Builder { private Product product = null; public ConcreteBuilder1() { this.product = new Product(); } @Override public void buildPart1() { product.add("Hamburger", "2"); } @Override public void buildPart2() { product.add("Drink", "1"); } @Override public void getProducts() { product.getProducts(); } }
ConcreteBuilder2.java:
package com.rick.designpattern.builder; /** * 具体建造者类:餐馆员工,汉堡1个+饮料2个 * <p> * Created by MyPC on 2017/6/9. */ public class ConcreteBuilder2 extends Builder { private Product product = null; public ConcreteBuilder2() { this.product = new Product(); } @Override public void buildPart1() { product.add("Hamburger", "1"); } @Override public void buildPart2() { product.add("Drink", "2"); } @Override public void getProducts() { product.getProducts(); } }
ConcreteBuilder3.java:
package com.rick.designpattern.builder; /** * Created by MyPC on 2017/6/9. */ public class ConcreteBuilder3 extends Builder { private Product product = null; public ConcreteBuilder3() { this.product = new Product(); } @Override public void buildPart1() { product.add("Hamburger", "3"); } @Override public void getProducts() { product.getProducts(); } }
Product.java:
package com.rick.designpattern.builder; import java.util.*; /** * Created by MyPC on 2017/6/9. */ public class Product { private Map<String, String> products = new HashMap<String, String>(); /** * 添加具体产品 */ public void add(String name, String value) { products.put(name, value); } public void getProducts() { Set<String> keys = products.keySet(); for (String key : keys) { System.out.println(key + "=" + products.get(key)); } } }