目录
什么是建造者模式?
建造者模式也属于创建型模式,它提供了一种创建对象的最佳方式。将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。主要目的是在用户不知道对象的建造过程和细节的情况下就可以直接创建复杂的对象。用户只需要给出指定复杂对象的类型和内容,建造者模式负责按顺序创建复杂对象,把内部的建造过程和细节隐藏起来。
简单代码示例:
/**
* 创建对象类
*/
public class Product {
private String GenerateObj1;
private String GenerateObj2;
private String GenerateObj3;
private String GenerateObj4;
public String getGenerateObj1() {
return GenerateObj1;
}
public String getGenerateObj2() {
return GenerateObj2;
}
public String getGenerateObj3() {
return GenerateObj3;
}
public String getGenerateObj4() {
return GenerateObj4;
}
public void setGenerateObj1(String generateObj1) {
GenerateObj1 = generateObj1;
}
public void setGenerateObj2(String generateObj2) {
GenerateObj2 = generateObj2;
}
public void setGenerateObj3(String generateObj3) {
GenerateObj3 = generateObj3;
}
public void setGenerateObj4(String generateObj4) {
GenerateObj4 = generateObj4;
}
@java.lang.Override
public java.lang.String toString() {
return "GenerateObj{" +
"GenerateObj1='" + GenerateObj1 + '\'' +
", GenerateObj2='" + GenerateObj2 + '\'' +
", GenerateObj3='" + GenerateObj3 + '\'' +
", GenerateObj4='" + GenerateObj4 + '\'' +
'}';
}
}
/**
* 抽象类:创建对象抽象方法
*/
public abstract class Builder {
abstract void ObjBuilder1();
abstract void ObjBuilder2();
abstract void ObjBuilder3();
abstract void ObjBuilder4();
//生成对象
abstract Product getGenerateObj();
}
/**
* 创建对象具体方法
*/
public class Worker extends Builder{
private Product product;
public Worker() {
this.product = new Product();
}
@Override
void ObjBuilder1() {
product.setGenerateObj1("ObjBuilder1");
System.out.println("ObjBuilder1");
}
@Override
void ObjBuilder2() {
product.setGenerateObj2("ObjBuilder2");
System.out.println("ObjBuilder2");
}
@Override
void ObjBuilder3() {
product.setGenerateObj3("ObjBuilder3");
System.out.println("ObjBuilder3");
}
@Override
void ObjBuilder4() {
product.setGenerateObj4("ObjBuilder4");
System.out.println("ObjBuilder4");
}
@Override
Product getGenerateObj() {
return product;
}
}
/**
*创建具体对象由这个类决定
*/
public class Director {
//指定按照顺序创建对象
public static Product build(Builder builder){
builder.ObjBuilder1();
builder.ObjBuilder2();
builder.ObjBuilder3();
builder.ObjBuilder4();
return builder.getGenerateObj();
}
}
public class Test {
public static void main(String[] args) {
Product product =Director.build(new Worker());
System.out.printf(product.toString());
}
}
==============================
==============结果============
ObjBuilder1
ObjBuilder2
ObjBuilder3
ObjBuilder4
GenerateObj{GenerateObj1='ObjBuilder1', GenerateObj2='ObjBuilder2', GenerateObj3='ObjBuilder3', GenerateObj4='ObjBuilder4'}
优化
通过静态内部类方式实现零件无序装配构造,这种方式使用更加灵活,更符合定义。内部有复杂对象的默认实现,使用时可以根据用户需求自由定义更改内容,并且无需改变具体的构造方式。就可以生产出不同复杂产品
代码示例:
/**
* 创建对象
*/
public class Product1 {
private String builder1 = "a";
private String builder2 = "b";
private String builder3 = "c";
private String builder4 = "d";
public String getBuilder1() {
return builder1;
}
public String getBuilder2() {
return builder2;
}
public String getBuilder3() {
return builder3;
}
public String getBuilder4() {
return builder4;
}
public void setBuilder1(String builder1) {
this.builder1 = builder1;
}
public void setBuilder2(String builder2) {
this.builder2 = builder2;
}
public void setBuilder3(String builder3) {
this.builder3 = builder3;
}
public void setBuilder4(String builder4) {
this.builder4 = builder4;
}
@Override
public String toString() {
return "Product1{" +
"builder1='" + builder1 + '\'' +
", builder2='" + builder2 + '\'' +
", builder3='" + builder3 + '\'' +
", builder4='" + builder4 + '\'' +
'}';
}
}
/**
* 抽象类:创建对象抽象方法
*/
public abstract class Builder1 {
abstract Builder1 builder1(String val);
abstract Builder1 builder2(String val);
abstract Builder1 builder3(String val);
abstract Builder1 builder4(String val);
//生成对象
abstract Product1 getGenerateProduct();
}
====================================================================
====================================================================
实现:
/**
* 创建对象具体方法
*/
public class Worker1 extends Builder1 {
private Product1 product;
public Worker1() {
this.product = new Product1();
}
@Override
Builder1 builder1(String val) {
product.setBuilder1(val);
return this;
}
@Override
Builder1 builder2(String val) {
product.setBuilder2(val);
return this;
}
@Override
Builder1 builder3(String val) {
product.setBuilder3(val);
return this;
}
@Override
Builder1 builder4(String val) {
product.setBuilder4(val);
return this;
}
@Override
Product1 getGenerateProduct() {
return product;
}
}
测试与结果:
public class Test1 { public static void main(String[] args) { Worker1 worker1 = new Worker1(); // Product1 generateProduct = worker1.getGenerateProduct(); // System.out.println(generateProduct.toString()); //链式编程 Product1 generateProduct = worker1.builder1("g").builder3("j").getGenerateProduct(); System.out.println(generateProduct.toString()); } } ========================================================== ========================结果============================== Product1{builder1='g', builder2='b', builder3='j', builder4='d'} Process finished with exit code 0