java设计模式-建造者模式(两种构造者模式)

经典的建造者模式

结构UML

java设计模式-建造者模式(两种构造者模式)_第1张图片

优点

暂时不晓得

劣势

暂时不晓得

使用情况

暂时不晓得

代码示例

实际需要生产的产品,有两个属性:

public class Product {
	private int propertiesA;
	private String propertiesB;

	public int getPropertiesA() {
		return propertiesA;
	}

	public void setPropertiesA(int propertiesA) {
		this.propertiesA = propertiesA;
	}

	public String getPropertiesB() {
		return propertiesB;
	}

	public void setPropertiesB(String propertiesB) {
		this.propertiesB = propertiesB;
	}

	@Override
	public String toString() {
		return "Product [propertiesA=" + propertiesA + ", propertiesB=" + propertiesB + "]";
	}

}

一个Buidler,用于规范要构造的产品有哪些属性,这里有属性A和属性B:

public interface Builder {
	public void propertiesA();
	public void propertiesB();
	
	public Product createProduct();
}

真正的建造者,用于实际建造产品,对相应的产品赋值,个人理解,如果你需要不同的属性类别的,可以实现同样的接口,设置新的属性,当然也可以进行变种,将属性通过外部传入而不是在类似propertiesA()方法中写死:

public class ConcreteBuilder implements Builder {

	private Product product;

	public ConcreteBuilder() {
		this.product = new Product();
	}

	@Override
	public void propertiesA() {
		// TODO Auto-generated method stub
		this.product.setPropertiesA(12345);
	}

	@Override
	public void propertiesB() {
		// TODO Auto-generated method stub
		this.product.setPropertiesB("BBB");
	}

	@Override
	public Product createProduct() {
		// TODO Auto-generated method stub
		return this.product;
	}

}

一个引导者,与外部客户端进行调用沟通:

public class Director {
	private Builder builder;

	private Director() {
	}

	public Director(Builder builder) {
		this.builder = builder;
	}

	public Product createProduct() {
		this.builder.propertiesA();
		this.builder.propertiesB();
		return this.builder.createProduct();
	}

}

最后,我们进行测试,也就是客户端调用:

	public static void main(String[] args) {
		
		//具体使用的builer
		Builder builder = new ConcreteBuilder();

		//使用一个引导者直接与客户端交互
		Director director = new Director(builder);

		//通过引导者来生产我们需要的产品
		Product product = director.createProduct();

		System.out.println("product.toString():"+product.toString());
	}

变种的建造者模式

变种的建造者模式即我们经常看到的生产链,build、build、build后,最后再通过build进行生成,代码很简单,个人感觉需要注意的主要是作用域及在使用过程中的变种:

public class Pizza {
	private String name;
	private String price;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}

	public static Builder newBuilder() {
		return new Builder();
	}

	//私有空构造器
	private Pizza() {
	}
	
	//带参构造器为Builder使用
	private Pizza(Builder builder) {
		this.name = builder.name;
		this.price = builder.price;
	}
	
	public static final class Builder {
		private String name;
		private String price;
		
		public Builder name(String name) {
			this.name = name;
			return this;
		}
		
		public Builder price(String price) {
			this.price = price;
			return this;
		}
		
		public Pizza build() {
			return new Pizza(this);
		}
	}
	
    public static void main(String[] args) {

    	Pizza pizza = Pizza.newBuilder().name("chicken cheese").build();
		System.out.println(pizza.getName());
	}

}

 

你可能感兴趣的:(#,设计模式)