一、使用场景
1、当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
2、当构造过程必须允许被构造的对象有不同的表示时。
二、通用类图
三、举例
假设有一个手机厂商,为客户生产手机,整个手机由硬件、操作系统和应用程序组成,用户可能需要iPhone手机,或者Android手机。
产品角色Product——Phone:
package com.wz.builder;
public class Phone {
private String hardware;
private String OS;
private String app;
public String getHardware() {
return hardware;
}
public void setHardware(String hardware) {
this.hardware = hardware;
}
public String getOS() {
return OS;
}
public void setOS(String oS) {
OS = oS;
}
public String getApp() {
return app;
}
public void setApp(String app) {
this.app = app;
}
}
具体产品1——Android手机:
package com.wz.builder;
public class AndroidPhone extends Phone{
public AndroidPhone(){
System.out.println("生产 Android手机");
}
}
具体产品2——iPhone手机:
package com.wz.builder;
public class IPhone extends Phone{
public IPhone(){
System.out.println("生产iPhone手机");
}
}
抽象建造者类Builder——PhoneBuilder:
package com.wz.builder;
public interface PhoneBuilder {
public void buildHardware();
public void buildOS();
public void buildApp();
public Phone retrievePhone();
}
具体建造者类ConcreteBuilder1——AndroidPhoneBuilder:
package com.wz.builder;
public class AndroidPhoneBuilder implements PhoneBuilder{
Phone phone;
public AndroidPhoneBuilder() {
phone = new AndroidPhone();
}
@Override
public void buildHardware() {
// TODO Auto-generated method stub
System.out.println("build Android手机的硬件");
}
@Override
public void buildOS() {
// TODO Auto-generated method stub
System.out.println("build Android手机的系统");
}
@Override
public void buildApp() {
// TODO Auto-generated method stub
System.out.println("build Android手机的应用程序");
}
@Override
public Phone retrievePhone() {
// TODO Auto-generated method stub
return phone;
}
}
具体建造者类ConcreteBuilder2——IPhoneBuilder:
package com.wz.builder;
public class IPhoneBuilder implements PhoneBuilder{
Phone phone;
public IPhoneBuilder(){
phone = new IPhone();
}
@Override
public void buildHardware() {
// TODO Auto-generated method stub
System.out.println("build iPhone手机的硬件");
}
@Override
public void buildOS() {
// TODO Auto-generated method stub
System.out.println("build iPhone手机的硬件");
}
@Override
public void buildApp() {
// TODO Auto-generated method stub
System.out.println("build iPhone手机的硬件");
}
@Override
public Phone retrievePhone() {
// TODO Auto-generated method stub
return phone;
}
}
导演者类Director——PhoneDirector:
package com.wz.builder;
public class PhoneDirector {
public Phone constructPhone(PhoneBuilder phoneBuilder){
phoneBuilder.buildHardware();
phoneBuilder.buildOS();
phoneBuilder.buildApp();
return phoneBuilder.retrievePhone();
}
}
客户端类Client——Client:
package com.wz.builder;
public class Client {
public static void main(String[] args) {
PhoneDirector phoneDirector = new PhoneDirector();
phoneDirector.constructPhone(new AndroidPhoneBuilder());
phoneDirector.constructPhone(new IPhoneBuilder());
}
}
执行结果:
生产 Android手机
build Android手机的硬件
build Android手机的系统
build Android手机的应用程序
生产iPhone手机
build iPhone手机的硬件
build iPhone手机的硬件
build iPhone手机的硬件
参考:
http://haolloyin.blog.51cto.com/1177454/333155/
http://blog.csdn.net/jason0539/article/details/44992733
http://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html
http://www.cnblogs.com/java-my-life/archive/2012/04/07/2433939.html