实体类
package factorymode.simplefactory.builder;
public class MobilePhone {
//手机屏幕
private String screen;
//电池
private String battery;
//话筒
private String microphone;
//听筒
private String phoneReceiver;
//机身
private String phoneBody;
public String getScreen() {
return screen;
}
public void setScreen(String screen) {
this.screen = screen;
}
public String getBattery() {
return battery;
}
public void setBattery(String battery) {
this.battery = battery;
}
public String getMicrophone() {
return microphone;
}
public void setMicrophone(String microphone) {
this.microphone = microphone;
}
public String getPhoneReceiver() {
return phoneReceiver;
}
public void setPhoneReceiver(String phoneReceiver) {
this.phoneReceiver = phoneReceiver;
}
public String getPhoneBody() {
return phoneBody;
}
public void setPhoneBody(String phoneBody) {
this.phoneBody = phoneBody;
}
@Override
public String toString() {
return "MobilePhone{" +
"screen='" + screen + '\'' +
", battery='" + battery + '\'' +
", microphone='" + microphone + '\'' +
", phoneReceiver='" + phoneReceiver + '\'' +
", phoneBody='" + phoneBody + '\'' +
'}';
}
}
建造者接口
package factorymode.simplefactory.builder;
public interface IBuildPhone {
/**
* 建造手机屏幕
*/
void buildScreen();
/**
* 建造手机电池
*/
void buildBattery();
/**
* 建造手机听筒
*/
void buildMicrophone();
/**
* 建造手机话筒
*/
void buildPhoneReceiver();
/**
* 建造手机机身
*/
void buildPhoneBody();
public MobilePhone build();
}
IPhoneX 建造者
package factorymode.simplefactory.builder;
public class IPhoneX implements IBuildPhone {
private MobilePhone mobilePhone;
public IPhoneX() {
mobilePhone = new MobilePhone();
}
@Override
public void buildScreen() {
mobilePhone.setScreen("OLED");
}
@Override
public void buildBattery() {
mobilePhone.setBattery("2700");
}
@Override
public void buildMicrophone() {
mobilePhone.setMicrophone("listen");
}
@Override
public void buildPhoneReceiver() {
mobilePhone.setPhoneReceiver("receiver");
}
@Override
public void buildPhoneBody() {
mobilePhone.setPhoneBody("body");
}
@Override
public MobilePhone build() {
return mobilePhone;
}
}
指导者
package factorymode.simplefactory.builder;
public class Director {
public MobilePhone createMobilePhone(IBuildPhone iBuildPhone) {
iBuildPhone.buildBattery();
iBuildPhone.buildMicrophone();
iBuildPhone.buildPhoneBody();
iBuildPhone.buildPhoneReceiver();
iBuildPhone.buildScreen();
return iBuildPhone.build();
}
}
测试类
package factorymode.simplefactory.builder;
public class Builder1 {
public static void main(String[] args) {
Director director = new Director();
MobilePhone phone = director.createMobilePhone(new IPhoneX());
System.out.println(phone);
}
}