建造者模式Builder(创建型)

参考文档:

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));
        }
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值