建造者模式
将一个对象的构建与它的表示分离,使得同样的构建过程创建出不同的表示。
模型图
代码
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);
}
}
小结
主要用来构造复杂对象,这些对象的构建顺序通常是稳定的,但内部构建通常面临着复杂的变化。
建造代码与表示代码相互分离,隐藏对象组装顺序,改变产品表示,只需要更改建造者就可以了。
当创建对象的算法,应该独立于改对象的组成部分以及他们的装配方式时适用。