java设计模式3——建造者模式

java设计模式3——建造者模式

1、建造者模式介绍:

建造者模式属于创建型模式,他提供了一种创建对象得最佳方式

定义:

将一个复杂对象的构建和与它的表示分离,使得同样的构建过程可以创建不同的表示

主要作用:

  1. 用户不知道对象的建造过程和细节的情况下就可以直接创建复杂的对象
  2. 用户只需要给出指定复杂对象的类型和内容,建造者模式负责按顺序创建复杂的对象(把内部的建造过程和细节隐藏了起来)

举例:

2、建造者模式角色分析

3、方式一:建造模式的常规用法(有指挥)

3.1、构建抽象的建造者(图纸)

package com.xgp.company.第三种_建造者模式.常规用法_套餐用法;

/**
 * 抽象的建造者
 */
public abstract class Builder {

    abstract void buildA();     //地基
    abstract void buildB();     //钢筋水泥
    abstract void buildC();     //铺电线
    abstract void buildD();     //粉刷

    //完工:等到产品
    abstract Product getProduct();
}

3.2、定义产品(房子)

package com.xgp.company.第三种_建造者模式.常规用法_套餐用法;

//产品:房子
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.3、定义具体的建造者(工人)

package com.xgp.company.第三种_建造者模式.常规用法_套餐用法;

//具体的建造者
public class Worker extends Builder {

    private Product product;

    public Worker() {
        product = new Product();
    }

    @Override
    void buildA() {
        product.setBuildA("铺地基");
        System.out.println("铺地基");
    }

    @Override
    void buildB() {
        product.setBuildB("搭钢筋");
        System.out.println("搭钢筋");
    }

    @Override
    void buildC() {
        product.setBuildC("铺电线");
        System.out.println("铺电线");
    }

    @Override
    void buildD() {
        product.setBuildD("粉刷墙壁");
        System.out.println("粉刷墙壁");
    }

    @Override
    Product getProduct() {
        return product;
    }
}

3.4、定义指挥者,指挥工人建造

package com.xgp.company.第三种_建造者模式.常规用法_套餐用法;

/**
 * 指挥:核心,负责指挥构建一个工程,工程如何构建,由他来决定
 */
public class Director {

    //指挥工人创建房子
    public Product build(Builder builder) {
        //创建产品
        builder.buildA();
        builder.buildB();
        builder.buildC();
        builder.buildD();
        return builder.getProduct();
    }
}

3.5、创建测试类,进行建造

package com.xgp.company.第三种_建造者模式.常规用法_套餐用法;

public class Test {
    public static void main(String[] args) {
        //指挥
        Director director = new Director();
        //指挥具体的工人完成创建
        Product build = director.build(new Worker());
        System.out.println(build.toString());
    }
}

结果:

铺地基
搭钢筋
铺电线
粉刷墙壁
Product{buildA='铺地基', buildB='搭钢筋', buildC='铺电线', buildD='粉刷墙壁'}

4、方式二:用户可以自定义套餐和选择默认套餐(没有指挥者)

4.1、定义抽象的建造者

package com.xgp.company.第三种_建造者模式.通过静态内部类来实现;

/**
 * 抽象的建造者
 */
public abstract class Builder {

    abstract Builder buildA(String msg);     //汉堡
    abstract Builder buildB(String msg);     //可乐
    abstract Builder buildC(String msg);     //薯条
    abstract Builder buildD(String msg);     //甜点

    //完工:得到产品
    abstract Product getProduct();
}

4.2、定义产品和默认套餐

package com.xgp.company.第三种_建造者模式.通过静态内部类来实现;

/**
 * 套餐
 */
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.3、定义具体的建造者(服务员)

package com.xgp.company.第三种_建造者模式.通过静态内部类来实现;

//具体的建造者
public class Worker extends Builder {

    private Product product;

    public Worker() {
        product = new Product();
    }


    @Override
    Builder buildA(String msg) {
        product.setBuildA(msg);
        return this;
    }

    @Override
    Builder buildB(String msg) {
        product.setBuildB(msg);
        return this;
    }

    @Override
    Builder buildC(String msg) {
        product.setBuildC(msg);
        return this;
    }

    @Override
    Builder buildD(String msg) {
        product.setBuildD(msg);
        return this;
    }

    @Override
    Product getProduct() {
        return product;
    }
}

4.4、定义顾客,进行点餐

package com.xgp.company.第三种_建造者模式.通过静态内部类来实现;

public class Test {
    public static void main(String[] args) {
        //服务员
        Worker worker = new Worker();
        Product product = worker.getProduct();
        System.out.println(product.toString());

        //自定义套餐
        product = worker.buildA("aaa").buildB("bbb").buildC("cccc").buildD("ddd").getProduct();
        System.out.println(product.toString());
    }
}

运行结果:

Product{buildA='汉堡', buildB='可乐', buildC='薯条', buildD='甜点'}
Product{buildA='aaa', buildB='bbb', buildC='cccc', buildD='ddd'}

5、优点分析:

6、缺点分析:

7、应用场景:

8、建造者模式与抽象工厂模式的比较

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章