1. 程式人生 > >【設計模式】建造者模式(建立型)

【設計模式】建造者模式(建立型)

建造者模式

     將一個物件的構建與它的表示分離,使得同樣的構建過程創建出不同的表示。

模型圖

 程式碼

public abstract class Builder {
	public abstract void BuilderPartA();
	public abstract void BuilderPartB();
	public abstract Product getResult();
}

 

public class ConcreteBuilder1 extends Builder{
	Product product=new Product();
	@Override
	public void BuilderPartA() {
		// TODO Auto-generated method stub
		product.add("BuilderPartA");
	}

	@Override
	public void BuilderPartB() {
		// TODO Auto-generated method stub
		product.add("BuilderPartB");
	}

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

 

public class ConcreteBuilder2 extends Builder{
	Product product=new Product();
	@Override
	public void BuilderPartA() {
		// TODO Auto-generated method stub
		product.add("BuilderPartX");
	}

	@Override
	public void BuilderPartB() {
		// TODO Auto-generated method stub
		product.add("BuilderPartY");
	}

	@Override
	public Product getResult() {
		// TODO Auto-generated method stub
		return product;
	}
}
public class Director {
   private Builder builder=null;
   
   public Director(Builder builder){
	   this.builder=builder;
	   this.builder.BuilderPartA();
	   this.builder.BuilderPartB();
   }
   
   public Product getResult(){
	   return builder.getResult();
   }
}

 

public class Product {
	List<String> properties = new ArrayList<String>();

	public void add(String e) {
		properties.add(e);
	}

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

 

public class Test {
	public static void main(String[] args) {
		Builder builder=new ConcreteBuilder1();
	    Director director=new Director(builder);
	    Product result = director.getResult();
	    System.out.println(result);
	}
}

 

 

 案例 

需求  建立一個人物畫像 要求必須的身體部位不能漏掉,具體身體部位的修飾可以隨意,但是流程要統一

分析與設計  修飾可以隨意,但是流程順序要統一,又是建立型的,利用建立者模式

模型圖

 程式碼

public abstract class PersonBuilder {
	public abstract void BuildHand();
	public abstract void BuildLeg();
	public abstract void BuildHead();
	public abstract void BuildBoby();
	public abstract Person getPerson();
}
public class FatPersonBuilder extends PersonBuilder{
	Person person=new Person();
	@Override
	public void BuildHand() {
		person.setHand("FatPersonBuilder Hand");
	}

	@Override
	public void BuildLeg() {
		person.setLeg("FatPersonBuilder leg");
		
	}

	@Override
	public void BuildHead() {
		person.setHead("FatPersonBuilder head");
	}

	@Override
	public void BuildBoby() {
		person.setBoby("FatPersonBuilder body");
	}

	@Override
	public Person getPerson() {
		// TODO Auto-generated method stub
		return this.person;
	}

}

 

 

public class ThinPersonBuiler extends PersonBuilder {
	Person person=new Person();
	@Override
	public void BuildHand() {
		person.setHand("ThinPersonBuiler Hand");
	}

	@Override
	public void BuildLeg() {
		person.setLeg("ThinPersonBuiler leg");
		
	}

	@Override
	public void BuildHead() {
		person.setHead("ThinPersonBuiler head");
	}

	@Override
	public void BuildBoby() {
		person.setBoby("ThinPersonBuiler body");
	}

	@Override
	public Person getPerson() {
		// TODO Auto-generated method stub
		return this.person;
	}

}
public class PersonDirector {
   private PersonBuilder builder=null;
   
   public PersonDirector(PersonBuilder builder){
	   this.builder=builder;
   }
   
   public Person getPerson(){
	   builder.BuildHead();
	   builder.BuildHand();
	   builder.BuildBoby();
	   builder.BuildLeg();
	   return this.builder.getPerson();
   }
}
public class Person {
	private String hand;
	private String leg;
	private String head;
	private String boby;

	public String getHand() {
		return hand;
	}

	public void setHand(String hand) {
		this.hand = hand;
	}

	@Override
	public String toString() {
		return "Person [hand=" + hand + ", leg=" + leg + ", head=" + head
				+ ", boby=" + boby + "]";
	}

	public String getLeg() {
		return leg;
	}

	public void setLeg(String leg) {
		this.leg = leg;
	}

	public String getHead() {
		return head;
	}

	public void setHead(String head) {
		this.head = head;
	}

	public String getBoby() {
		return boby;
	}

	public void setBoby(String boby) {
		this.boby = boby;
	}

}

 

public class Test {
   public static void main(String[] args) {
	   PersonBuilder builder=new ThinPersonBuiler();
	   PersonDirector director=new PersonDirector(builder);
	   Person person = director.getPerson();
	   System.out.println(person);
   }
}

 

 

 小結

     主要用來構造複雜物件,這些物件的構建順序通常是穩定的,但內部構建通常面臨著複雜的變化。

     建造程式碼與表示程式碼相互分離,隱藏物件組裝順序,改變產品表示,只需要更改建造者就可以了。

     當建立物件的演算法,應該獨立於改物件的組成部分以及他們的裝配方式時適用。