目的
将一个负责对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示。
使用场景
- 当创建对象的算法应该被独立于该对象的组成部分以及他们的装配方式时。
- 当创建结构语序被构造的对象有不同的表示时。
具体描述
该模式主要是将建造这一个过程隐藏在一个director监视着后面,通过 调用监视者来达到生产的过程,我们只需要在调用监视者的过程中给定一个参数就能够决定生产哪一个产品,生产过程是一个不可见的过程,从而达到了很好的隔离效果。
项目结构如下:
image.png
phone是一个实体类,这就是我们要建造的产品product。
public class Phone {
private String name;
private int prize;
private String system;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
this.prize = prize;
}
public String getSystem() {
return system;
}
public void setSystem(String system) {
this.system = system;
}
@Override
public String toString() {
return "Phone{" +
"name='" + name + '\'' +
", prize=" + prize +
", system='" + system + '\'' +
'}';
}
}
PhoneBuilder是一个接口,在这里面定义了对于phone的各种操作。
public interface PhoneBuilder {
void setName();
void setMoney();
void setSystem();
Phone getPhone();
}
HuaweiPhoneBuilder和XiaomiPhoneBuilder是两个不同的建造者,是对于接口PhoneBuilder的一个具体实现。
public class HuaweiPhoneBuilder implements PhoneBuilder {
private Phone huaweiPhone;
public HuaweiPhoneBuilder(){
huaweiPhone = new Phone();
}
public void setName(){
huaweiPhone.setName("Huawei Mate 20");
}
public void setMoney(){
huaweiPhone.setPrize(5299);
}
public void setSystem(){
huaweiPhone.setSystem("EMUI");
}
public Phone getPhone(){
return huaweiPhone;
}
}
public class XiaomiPhoneBuilder implements PhoneBuilder {
private Phone xiaomiPhone;
public XiaomiPhoneBuilder(){
xiaomiPhone = new Phone();
}
public void setName(){
xiaomiPhone.setName("MI 9");
}
public void setMoney(){
xiaomiPhone.setPrize(2999);
}
public void setSystem(){
xiaomiPhone.setSystem("MIUI");
}
public Phone getPhone(){
return xiaomiPhone;
}
}
Director是一个监视者的角色,他封装了不同的PhoneBuilder,PhoneBuider对于Cilent是隐藏的,真正操作的是Director这一个类。
public class Director {
private PhoneBuilder phoneBuilder;
public Director(PhoneBuilder phoneBuilder){
this.phoneBuilder = phoneBuilder;
}
public void CreatePhone(){
phoneBuilder.setName();
phoneBuilder.setMoney();
phoneBuilder.setSystem();
}
public Phone getPhone(){
return phoneBuilder.getPhone();
}
}
Client是一个测试类
public class Client {
public static void main(String[] args){
Director director = new Director(new XiaomiPhoneBuilder());
director.CreatePhone();
Phone xiaomiPhone = director.getPhone();
System.out.println(xiaomiPhone.toString());
Director director1 = new Director(new HuaweiPhoneBuilder());
director1.CreatePhone();
Phone huaweiPhone = director1.getPhone();
System.out.println(huaweiPhone.toString());
}
在学习了https://www.jianshu.com/p/afe090b2e19c的文章之后,可以发现对于builder模式的一个改进地方就是使用内部类的方式来增加一些可选项然而不需要使用冗杂的set方法就可以实现。
实现方法就是在产品类中增加一个静态内部类Builder,这样的话就可以编写一些返回值为Builder自己的方法来不断增加其参数。实现方法如下:
public class Phone {
//必须项目
private String name;
private int prize;
private String system;
//非必须项目
private String NO;
private int version;
private int size;
private Phone(Builder builder){
this.name = builder.name;
this.prize = builder.prize;
this.system = builder.system;
this.NO = builder.NO; //手机序列号
this.version = builder.version; //安卓版本
this.size = builder.size; //手机大小
}
@Override
public String toString() {
return "Phone{" +
"name='" + name + '\'' +
", prize=" + prize +
", system='" + system + '\'' +
", NO='" + NO + '\'' +
", version=" + version +
", size=" + size +
'}';
}
public static class Builder{
private String name;
private int prize;
private String system;
private String NO;
private int version;
private int size;
public Builder(String name,int prize,String system){
this.name = name;
this.prize = prize;
this.system = system;
}
public Builder NO(String NO){
this.NO = NO;
return this;
}
public Builder version(int version){
this.version = version;
return this;
}
public Builder size(int size){
this.size = size;
return this;
}
public Phone build(){
return new Phone(this);
}
}
}
public class Cilent {
public static void main(String[] args){
Phone xiaomiPhone = new Phone.Builder("MI 9",2999,"MIUI").NO("123456").version(9).build();
System.out.println(xiaomiPhone.toString());
Phone huaweiPhone = new Phone.Builder("Mate 20",5299,"EMUI").NO("99999").size(20).build();
System.out.println(huaweiPhone.toString());
}
网友评论