亲宝软件园·资讯

展开

Java设计模式之建造者模式的示例详解

游坦之 人气:0

1 定义

建造者模式(Builder Patten):将一个复杂对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示。

建造者模式用于一步一步创建一个复杂的对象,他允许用户只通过指定复杂对象的类型和内容进行构建,用户不需要知道内部的具体构建细节。建造者模式属于对象创建型模式,根据翻译的不同,建造者模式又可称为生成器模式。

2 结构与角色

建造者模式包含4个角色

3 例子

建造楼房的过程中,有专业人员绘制图纸,图纸中讲述干什么,工人根据图纸去具体实现要干的事情,包工头根据实际情况去处理图纸中方法的执行,调配工人以何种顺序完成图纸上的要求。虽然包工头和图纸对建造高楼很有帮助,但是高楼平地起,却是由工人们一砖一瓦的搭建起来。

以上的这个例子很形象的讲述建造者模式执行过程,其中图纸就对应Builder(提供方法,要干什么),工人就对应ConcreteBuilder(具体去干方法中规定的事情),包工头就对应Director(以何种顺序干,调配工人),高楼就对应Product。

3.1 代码

3.1.1 Builder类

Builder类只提供抽象的方法,类似于图纸,只是告诉你建造高楼你需要做什么。

// 抽象方法,要做什么:建设一个大楼需要做什么
public abstract class Builder {
    abstract void builderA();
    abstract void builderB();
    abstract void builderC();
    abstract void builderD();
    abstract Product getProduct();
}

3.1.2 Worker类

工人是具体实现Builder提出的需求,比如水泥、钢筋等等。工人类直接产生产品(new)

package demo01;
// 知道了要做什么,就得有人来具体实现
public class Worker extends Builder{
    private Product product;
    public Worker() {
        product = new Product();
    }
    @Override
    void builderA() {
        product.setBuildA("打地基");
        System.out.println("打地基");
    }
    @Override
    void builderB() {
        product.setBuildB("钢筋混凝土");
        System.out.println("钢筋混凝土");
    }
    @Override
    void builderC() {
        product.setBuildC("盖房子");
        System.out.println("盖房子");
    }
    @Override
    void builderD() {
        product.setBuildD("油漆、粉刷");
        System.out.println("油漆、粉刷");
    }
    Product getProduct()
    {
        return product;
    }
}

3.1.3 Product类

当产品的本身的属性一一被满足的时候,产品才会产生。

public class Product {
    private String buildA;
    private String buildB;
    private String buildC;
    private String buildD;
    public String getBuildA() {
        return buildA;
    }
    public void setBuildA(String buildA) {
        this.buildA = buildA;
    }
    public String getBuildB() {
        return buildB;
    }
    public void setBuildB(String buildB) {
        this.buildB = buildB;
    }
    public String getBuildC() {
        return buildC;
    }
    public void setBuildC(String buildC) {
        this.buildC = buildC;
    }
    public String getBuildD() {
        return buildD;
    }
    public void setBuildD(String buildD) {
        this.buildD = buildD;
    }
    @Override
    public String toString() {
        return "Product{" +
                "buildA='" + buildA + '\'' +
                ", buildB='" + buildB + '\'' +
                ", buildC='" + buildC + '\'' +
                ", buildD='" + buildD + '\'' +
                '}';
    }
}

3.1.4 Director类

包工头,安排工人怎么去做,比如今天下雨,不打水泥了,去抬钢筋,今天不下雨了,去打地基。

// 指挥的作用是,以什么样的顺序,完成产品的制作
public class Director {
    public Product getProduct(Builder builder){
        builder.builderA();
        builder.builderB();
        builder.builderC();
        builder.builderD();
        return builder.getProduct();
    }
}

3.1.5 Test测试类

public class Test01 {
    public static void main(String[] args) {
        Director director = new Director();
        Product product = director.getProduct(new Worker());
        System.out.println(product.toString());
    }
}

指挥者调整顺序可以得到不同的产品,或者不同的效果(虽然这个没体现出来)

4 不使用指挥者的例子

去餐厅吃饭,餐厅里一般有默认的套餐,或者自己(用户)点餐,点餐之后,服务员根据订单负责将食品端上来,然后你大快朵颐,享受美食带来的快感…

在这一场景里,就没有指挥者,指挥权到达了用户的手里。上面3中的场景,指挥权就不能到用户手里,你不能让人家掏钱还得让人家自己建(除非是老板)。

4.1 Builder类

public abstract class Builder {
    abstract Builder builderA(String msg);
    abstract Builder builderB(String msg);
    abstract Builder builderC(String msg);
    abstract Builder builderD(String msg);
    abstract Product getProduct();
}

4.2 Worker类

public class Worker extends Builder {
    private Product product;
    public Worker() {
         product = new Product();
    }
    Builder builderA(String msg) {
        product.setBuildA(msg);
        return this;
    }
    Builder builderB(String msg) {
        product.setBuildB(msg);
        return this;
    }
    Builder builderC(String msg) {
        product.setBuildC(msg);
        return this;
    }
    Builder builderD(String msg) {
        product.setBuildD(msg);
        return this;
    }
    Product getProduct() {
        return product;
    }
}

4.3 Product类

public class Product {
    private String buildA = "炸鸡";
    private String buildB = "汉堡";
    private String buildC = "可乐";
    private String buildD = "大蒜";
    public String getBuildA() {
        return buildA;
    }
    public void setBuildA(String buildA) {
        this.buildA = buildA;
    }
    public String getBuildB() {
        return buildB;
    }
    public void setBuildB(String buildB) {
        this.buildB = buildB;
    }
    public String getBuildC() {
        return buildC;
    }
    public void setBuildC(String buildC) {
        this.buildC = buildC;
    }
    public String getBuildD() {
        return buildD;
    }
    public void setBuildD(String buildD) {
        this.buildD = buildD;
    }
    @Override
    public String toString() {
        return "Product{" +
                "buildA='" + buildA + '\'' +
                ", buildB='" + buildB + '\'' +
                ", buildC='" + buildC + '\'' +
                ", buildD='" + buildD + '\'' +
                '}';
    }
}

4.4 Test类

public class Test {
    public static void main(String[] args) {
        Worker worker = new Worker();
        System.out.println(worker.builderA("全家桶")
                .getProduct());
    }
}

上面的代码例子中:Worker就相当于服务员,记录你点了什么菜,并生成账单(Product里的信息),反馈给用户产品。

5 适用场景

6 优缺点

6.1 优点

6.2 缺点

加载全部内容

相关教程
猜你喜欢
用户评论