浅谈设计模式——建造者模式(Builder)

建造者模式(Builder Pattern)

简介

建造者模式也叫生成器模式。

Separate the construction of a complex object from its representation so that the same construction process can create different representation。

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

UML图

builder

角色:

  • 抽象建造者(Builder):提供一个接口用于创建产品的各个部件。
  • 具体建造者(ConcreteBuilder):创建对象时提供获取创建好的产品(Product)的接口。
  • 产品(Product):建造中的复杂对象。
  • 导演者(Director):通过提供的接口创建接口。

代码示例:

- Product类

package com.designpattern.test.builder;

/**
 * @author gzs
 */
public class Product {
    //产品类的业务处理方法

    private String size;
    private String name;
    private String price;
    private String address;
    private String owner;
    private String country;

    @Override
    public String toString() {
        return "Product{" +
                "size='" + size + '\'' +
                ", name='" + name + '\'' +
                ", price='" + price + '\'' +
                ", address='" + address + '\'' +
                ", owner='" + owner + '\'' +
                ", country='" + country + '\'' +
                '}';
    }

    public void setSize(String size) {
        this.size = size;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

Builder类

package com.designpattern.test.builder;

public abstract class Builder {
    public abstract Builder setPart1();
    public abstract Builder setPart2();
    public abstract Builder setPart3();
    public abstract Builder setPart4();
    public abstract Builder setPart5();
    //...
    public abstract Product build();

}

ConcreteBuilder类

package com.designpattern.test.builder;

public class ConcreteBuilder1 extends Builder{
    private Product product;
    public ConcreteBuilder1() {
        this.product = new Product();
    }

    @Override
    public Builder setPart1() {
        this.product.setOwner("gzs");
        return this;
    }

    @Override
    public Builder setPart2() {
        product.setCountry("China");
        return this;
    }

    @Override
    public Builder setPart3() {
        product.setName("HuaWei");
        return this;
    }

    @Override
    public Builder setPart4() {
        product.setPrice("10000");
        return this;
    }

    @Override
    public Builder setPart5() {
        product.setAddress("ShenZhen");
        return this;
    }



    @Override
    public Product build() {
        return this.product;
    }


}

ConcreteBuilder类,不同的实现,生产不同的产品

package com.designpattern.test.builder;

public class ConcreteBuilder2 extends Builder{
    private Product product;
    public ConcreteBuilder2() {
        this.product = new Product();
    }

    @Override
    public Builder setPart1() {
        product.setName("BYD");
        return this;
    }

    @Override
    public Builder setPart2() {
        product.setCountry("China");
        return this;
    }

    @Override
    public Builder setPart3() {
        product.setOwner("LiChuanFu");
        return this;
    }

    @Override
    public Builder setPart4() {
        product.setPrice("16000");
        return this;
    }

    @Override
    public Builder setPart5() {
        product.setAddress("shenzhen");
        return this;
    }

    @Override
    public Product build() {
        return this.product;
    }

}

Director类

package com.designpattern.test.builder;

public class Director {
    public static void main(String[] args) {
        Builder builder = new ConcreteBuilder1();
        //Builder builder = new ConcreteBuilder2();
		
		
        Product product = builder.setPart3().setPart1().setPart5().setPart2().setPart4().build();
        //链式建造产品,可以用不同零件组装
        //builder.setPart4().setPart1().setPart3().build();

        System.out.println(product.toString());

    }
}

总结

建造者模式关注的是零件类型和装配的顺序,这是与工厂模式不同之处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值