建造者设计模式也叫生成器设计模式,
Builder模式,将产品的内部表象和产品的生成过程分割开来,从而使一个建造过程生成具有不同的内部
表象的产品对象。建造模式使得产品内部表象可以独立的变化,客户不必知道产品内部组成的细节。建造模式可以强制实行一种分步骤进行的建造过程。
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
今天在看,Imageloader源码时,它里面也用了建造者设计模式,先看段代码
DisplayImageOptions imageOptions = new DisplayImageOptions.Builder() .cacheInMemory(true).cacheOnDisk(true) .resetViewBeforeLoading(true).considerExifParams(false) .bitmapConfig(Bitmap.Config.RGB_565).build();由于DisplayImageOptions代码太多,我就简单的抽取了一下:
public class DisplayImageOptions { public String name; public int age; public DisplayImageOptions(Builder builder) { super(); this.name = builder.name; this.age = builder.age; } public static class Builder{ private String name; private int age; public Builder setName(String name){ this.name = name; return this; } public Builder setAge(int age){ this.age = age; return this; } public DisplayImageOptions build() { return new DisplayImageOptions(this); } } }Test.java
public class Test { public static void main(String[] args) { DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder().setName("zhouguizhi").setAge(22).build(); System.out.println("name="+displayImageOptions.name+"---------age="+displayImageOptions.age); } }所谓表象和 产品的生成过程分割开来
表象就是DisplayImageOptions类中的成员变量 name,age
而生产过程就是Builder中setName() 方法么就是代表动作action,而setName()方法返回的就是所属类本身,
当然如果想用接口的形式也很简单,Builder.java 表示创建的行为
public interface Builder {
public void buildHead();
public void buildBody();
public void buildHand();
public void buildFoot();
public Person buildPerson();
}
Person.java创建的产品
public class Person {
private String head;
private String hand;
private String body;
private String foot;
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
public String getHand() {
return hand;
}
public void setHand(String hand) {
this.hand = hand;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getFoot() {
return foot;
}
public void setFoot(String foot) {
this.foot = foot;
}
}
PersonBuilder.java 创建Person也就是所谓创建产品的过程
public class PersonBuilder implements Builder{
Person person;
public PersonBuilder(){
person = new Person();
}
@Override
public void buildHead() {
person.setHead("build man's head!");
}
@Override
public void buildBody() {
person.setBody("build man's body!");
}
@Override
public void buildHand() {
person.setHand("build man's hand!");
}
@Override
public void buildFoot() {
person.setFoot("build man's food!");
}
@Override
public Person buildPerson() {
return person;
}
}
PersonDirector.java 这是真正创建产品了
//指挥创建
public Person ConstructPerson(Builder b)
{
b.buildFoot();
b.buildHand();
b.buildHead();
b.buildBody();
return b.buildPerson();
}
测试
public class BuilderDemo {
public static void main(String[] args) {
PersonDirector pd = new PersonDirector();
Person person = pd.ConstructPerson(new PersonBuilder());
System.out.println(person.getHead());
System.out.println(person.getBody());
System.out.println(person.getHand());
System.out.println(person.getFoot());
}
}