Builder模式--复杂的DEMO--Builder嵌套

优缺点

优点

  • 在建造者模式中, 客户端不必知道产品内部组成的细节,将产品本身与产品的创建过程解耦,使得相同的创建过程可以创建不同的产品对象。
  • 每一个具体建造者都相对独立,而与其他的具体建造者无关,因此可以很方便地替换具体建造者或增加新的具体建造者, 
    用户使用不同的具体建造者即可得到不同的产品对象。
  • 可以更加精细地控制产品的创建过程。将复杂产品的创建步骤分解在不同的方法中,使得创建过程更加清晰,也更方便使用程序来控制创建过程。
  • 增加新的具体建造者无须修改原有类库的代码,指挥者类针对抽象建造者类编程,系统扩展方便,符合“开闭原则”。

缺点

  • 使用Builder模式是肯定会增加代码量的。此外,尽管客户端的代码可读性明显改善,但随之而来的客户端代码变得更加冗长。
  • Builder会增加个类代码,这也意味着开发者在给类增加属性时有时会忘记给该属性添加支持的builder。

 

package com.ricky.builder.ch3;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @create 2016-07-06 15:38
 */
public class Car {
    private final String manufacturer;    //制造商
    private final String brand;   //品牌
    private final String model;   //型号
    private final String producingArea;   //产地
    private final String producingDate;   //生产时间
    private final Engine engine;
    private final Wheel wheel;
    private final Light light;

    Car(String manufacturer, String brand, String model, String producingArea,
        String producingDate, Engine engine, Wheel wheel, Light light) {
        this.manufacturer = manufacturer;
        this.brand = brand;
        this.model = model;
        this.producingArea = producingArea;
        this.producingDate = producingDate;
        this.engine = engine;
        this.wheel = wheel;
        this.light = light;
    }

    public static class Builder{
        private String manufacturer;    //制造商
        private String brand;   //品牌
        private String model;   //型号
        private String producingArea;   //产地
        private String producingDate;   //生产时间
        private Engine engine;
        private Wheel wheel;
        private Light light;

        public Builder(String manufacturer, String brand, String model) {
            this.manufacturer = manufacturer;
            this.brand = brand;
            this.model = model;
        }

        public Builder producingArea(String producingArea){
            this.producingArea = producingArea;
            return this;
        }

        public Builder producingDate(String producingDate){
            this.producingDate = producingDate;
            return this;
        }

        public Builder engine(Engine engine){
            this.engine = engine;
            return this;
        }
        public Builder wheel(Wheel wheel){
            this.wheel = wheel;
            return this;
        }
        public Builder light(Light light){
            this.light = light;
            return this;
        }

        public Car build(){

            return new Car(manufacturer, brand, model, producingArea, producingDate, engine, wheel, light);
        }
    }
}
package com.ricky.builder.ch3;

/**
 * 汽车发动机
 *
 * @author Ricky Fung
 * @create 2016-07-06 15:42
 */
public class Engine {
    private final String pl;  //排量
    private final String maxOutputPower; //最大输出功率
    private final int rpm;  //转速

    Engine(String pl, String maxOutputPower, int rpm) {
        this.pl = pl;
        this.maxOutputPower = maxOutputPower;
        this.rpm = rpm;
    }

    public static Builder custom(){

        return new Builder();
    }

    public static class Builder{
        private String pl;  //排量
        private String maxOutputPower; //最大输出功率
        private int rpm;  //转速

        public Builder pl(String pl){
            this.pl = pl;
            return this;
        }

        public Builder maxOutputPower(String maxOutputPower){
            this.maxOutputPower = maxOutputPower;
            return this;
        }

        public Builder rpm(int rpm){
            this.rpm = rpm;
            return this;
        }

        public Engine build(){

            return new Engine(pl, maxOutputPower, rpm);
        }
    }
}
package com.ricky.builder.ch3;

/**
 * 汽车轮子
 *
 * @author Ricky Fung
 * @create 2016-07-06 15:40
 */
public class Wheel {
    private String brand;
    private String producingDate;

    Wheel(String brand, String producingDate) {
        this.brand = brand;
        this.producingDate = producingDate;
    }

    public static Builder custom(){

        return new Builder();
    }

    public static class Builder{
        private String brand;
        private String producingDate;

        public Builder brand(String brand){
            this.brand = brand;
            return this;
        }

        public Builder producingDate(String producingDate){
            this.producingDate = producingDate;
            return this;
        }

        public Wheel build(){

            return new Wheel(brand, producingDate);
        }
    }
}
package com.ricky.builder.ch3;

/**
 * 车灯
 *
 * @author Ricky Fung
 * @create 2016-07-06 15:43
 */
public class Light {
    private String brand;
    private String structure;   //结构
    private String producingDate;

    Light(String brand, String structure, String producingDate) {
        this.brand = brand;
        this.structure = structure;
        this.producingDate = producingDate;
    }

    public static Builder custom(){

        return new Builder();
    }

    public static class Builder{
        private String brand;
        private String structure;   //结构
        private String producingDate;

        public Builder brand(String brand){
            this.brand = brand;
            return this;
        }

        public Builder structure(String structure){
            this.structure = structure;
            return this;
        }

        public Builder producingDate(String producingDate){
            this.producingDate = producingDate;
            return this;
        }

        public Light build(){

            return new Light(brand, structure, producingDate);
        }
    }
}

客户端调用:

package com.ricky.builder;

import com.ricky.builder.ch3.Car;
import com.ricky.builder.ch3.Engine;
import com.ricky.builder.ch3.Light;
import com.ricky.builder.ch3.Wheel;

/**
 * 汽车组装
 *
 * @author Ricky Fung
 * @create 2016-07-06 15:59
 */
public class CarDemo {

    public static void main(String[] args){

        Car car = new Car.Builder("Audi", "奥迪", "Q5")
                .producingArea("中国大陆")
                .producingDate("2016-07-01 00:00:00")
                .engine(Engine.custom()
                        .pl("2L")
                        .maxOutputPower("110kW")
                        .rpm(5400)
                        .build())
                .wheel(Wheel.custom()
                        .brand("AA")
                        .producingDate("2016-03-01 00:00:00")
                        .build())
                .light(Light.custom()
                        .brand("5A")
                        .structure("隔热玻璃")
                        .producingDate("2016-02-01 00:00:00")
                        .build())
                .build();

        System.out.println(car);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值