建造者模式的定义:是设计模式的一种,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
实用范围
1 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。 [1]
2 当构造过程必须允许被构造的对象有不同表示时。
角色
在这样的设计模式中,有以下几个角色:
1 builder:为创建一个产品对象的各个部件指定抽象接口。
2 ConcreteBuilder:实现Builder的接口以构造和装配该产品的各个部件,定义并明确它所创建的表示,并提供一个检索产品的接口。
3 Director:构造一个使用Builder接口的对象。
4 Product:表示被构造的复杂对象。ConcreteBuilder创建该产品的内部表示并定义它的装配过程,包含定义组成部件的类,包括将这些部件装配成最终产品的接口。
案例一:我要建房子请思考房子该如何建立呢!
package coms.Design.Mode.Builder; /** * 抽象的建造者模式 * 建造者的模式 */ public abstract class Builder { abstract void builderA(); //选地方 abstract void builderB(); //打地基 abstract void builderC(); //构建图纸 abstract void builderD(); //搭建房屋框架 abstract void builderE(); //开始建造 abstract void builderF(); //装修 abstract Product getProduct(); //得到产品 }
package coms.Design.Mode.Builder; public class Worker extends Builder { private Product product; public Worker() { this.product = new Product(); } @Override void builderA() { product.setBuStringA("选地方"); System.out.println("地方完毕"); } @Override void builderB() { product.setBuStringB("打地基"); System.out.println("地基打建造完毕"); } @Override void builderC() { product.setBuStringC("构建图纸"); System.out.println("图纸构建完毕"); } @Override void builderD() { product.setBuStringD("搭建房屋框架"); System.out.println("搭建房屋框架完毕"); } @Override void builderE() { product.setBuStringE("开始建造"); System.out.println("开始建造建造目标三层"); } @Override void builderF() { product.setBuStringF("装修"); System.out.println("装修资金60w"); } @Override Product getProduct() { return product; } }
package coms.Design.Mode.Builder; /** * 产品 房子 */ public class Product { private String buStringA; private String buStringB; private String buStringC; private String buStringD; private String buStringE; private String buStringF; public String getBuStringA() { return buStringA; } public void setBuStringA(String buStringA) { this.buStringA = buStringA; } public String getBuStringB() { return buStringB; } public void setBuStringB(String buStringB) { this.buStringB = buStringB; } public String getBuStringC() { return buStringC; } public void setBuStringC(String buStringC) { this.buStringC = buStringC; } public String getBuStringD() { return buStringD; } public void setBuStringD(String buStringD) { this.buStringD = buStringD; } public String getBuStringE() { return buStringE; } public void setBuStringE(String buStringE) { this.buStringE = buStringE; } public String getBuStringF() { return buStringF; } public void setBuStringF(String buStringF) { this.buStringF = buStringF; } @Override public String toString() { return "Product{" + "buStringA='" + buStringA + '\'' + ", buStringB='" + buStringB + '\'' + ", buStringC='" + buStringC + '\'' + ", buStringD='" + buStringD + '\'' + ", buStringE='" + buStringE + '\'' + ", buStringF='" + buStringF + '\'' + '}'; } }
package coms.Design.Mode.Builder; /** * 指挥者 构建工程 * 按照步骤建立房子 */ public class Director { /** * 步骤是指挥者来改变 * @param builder * @return */ public Product build(Builder builder){ builder.builderA(); builder.builderB(); builder.builderC(); builder.builderD(); builder.builderE(); builder.builderF(); return builder.getProduct(); } }
package coms.Design.Mode.Builder; public class Test { public static void main(String[] args) { //指挥者 Director director =new Director(); //指挥具体的工人 Product build=director.build(new Worker()); System.out.println(build.toString()); } }
案例二:我是老板要定制套餐该如何做呢!
package coms.Design.Mode.Builder2; //建造者 public abstract class Builder { /** * 抽象四个产品 * @return */ abstract Builder builderA(String msg); abstract Builder builderB(String msg); abstract Builder builderC(String msg); abstract Builder builderD(String msg); abstract Product getProduct(); }
package coms.Design.Mode.Builder2; /** * 建造者 */ public class Worker extends Builder { private Product product; public Worker() { product = new Product(); } @Override Builder builderA(String msg) { product.setBuidA(msg); return this; } @Override Builder builderB(String msg) { product.setBuidB(msg); return this; } @Override Builder builderC(String msg) { product.setBuidC(msg); return this; } @Override Builder builderD(String msg) { product.setBuidD(msg); return this; } @Override Product getProduct() { return product; } }
package coms.Design.Mode.Builder2; /** * 产品套餐 */ public class Product { private String BuidA="汉堡"; private String BuidB="可乐"; private String BuidC="食物"; private String BuidD="小鸡"; public String getBuidA() { return BuidA; } public void setBuidA(String buidA) { BuidA = buidA; } public String getBuidB() { return BuidB; } public void setBuidB(String buidB) { BuidB = buidB; } public String getBuidC() { return BuidC; } public void setBuidC(String buidC) { BuidC = buidC; } public String getBuidD() { return BuidD; } public void setBuidD(String buidD) { BuidD = buidD; } @Override public String toString() { return "Product{" + "BuidA='" + BuidA + '\'' + ", BuidB='" + BuidB + '\'' + ", BuidC='" + BuidC + '\'' + ", BuidD='" + BuidD + '\'' + '}'; } }
package coms.Design.Mode.Builder2; import com.sun.glass.ui.Window; public class Test { public static void main(String[] args) { //服务员 Worker worker=new Worker(); Product product=worker.getProduct(); //链式编程 步骤交给了客选择 Product product1=worker.builderA("全家桶").getProduct(); System.out.println(product.toString()); } }