赞
踩
建造者(Builder)模式的定义:指将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,这样的设计模式被称为建造者模式。它是将一个复杂的对象分解为多个简单的对象,然后一步一步构建而成。它将变与不变相分离,即产品的组成部分是不变的,但每一部分是可以灵活选择的。
该模式的主要优缺点如下:
其缺点如下:
建造者(Builder)模式和工厂模式的关注点不同:建造者模式注重零部件的组装过程,而工厂方法模式更注重零部件的创建过程,但两者可以结合使用。
应用场景:
建造者(Builder)模式的主要角色如下。
建造者模式的结构图:
具体的产品类,包含多个组成部件
public class Product { //有ABCD多个部件 private String buildA; private String buildB; private String buildC; private String buildD; public String getBuildA() { return buildA; } public void setBuildA(String buildA) { this.buildA = buildA; } public String getBuildB() { return buildB; } public void setBuildB(String buildB) { this.buildB = buildB; } public String getBuildC() { return buildC; } public void setBuildC(String buildC) { this.buildC = buildC; } public String getBuildD() { return buildD; } public void setBuildD(String buildD) { this.buildD = buildD; } @Override public String toString() { return "Product{" + "buildA='" + buildA + '\'' + ", buildB='" + buildB + '\'' + ", buildC='" + buildC + '\'' + ", buildD='" + buildD + '\'' + '}'; } }
抽象建造者,包含创建产品各个子部件的抽象方法
//抽象的建造者
public abstract class Builder {
//具体产品的各个子部件的抽象方法
abstract void buildA();//部件A
abstract void buildB();//部件B
abstract void buildC();//部件C
abstract void buildD();//部件B
//返回具体产品的方法
abstract Product getProduct();
}
具体建造者1,实现 Builder 接口,完成复杂产品的各个部件的具体创建方法。
//具体建造者,实现抽象建造者,完成复杂产品的各个部件的具体创建方法 public class Worker extends Builder{ private Product product; //提升作用域 //通过具体建造者的构造器生成具体产品的对象 public Worker(){ product = new Product(); } @Override void buildA() { product.setBuildA("完成部件A"); System.out.println("完成部件A"); } @Override void buildB() { product.setBuildB("完成部件B"); System.out.println("完成部件B"); } @Override void buildC() { product.setBuildC("完成部件C"); System.out.println("完成部件C"); } @Override void buildD() { product.setBuildD("完成部件D"); System.out.println("完成部件D"); } @Override Product getProduct() { System.out.println("完成产品ABCD的创建"); return product; } }
具体建造者2,实现 Builder 接口,完成复杂产品的各个部件的具体创建方法。
public class WorkerTwo extends Builder{ private Product product; public WorkerTwo(){ product = new Product(); } @Override void buildA() { product.setBuildA("完成部件A2"); System.out.println("完成部件A2"); } @Override void buildB() { product.setBuildB("完成部件B2"); System.out.println("完成部件B2"); } @Override void buildC() { product.setBuildC("完成部件C2"); System.out.println("完成部件C2"); } @Override void buildD() { product.setBuildD("完成部件D2"); System.out.println("完成部件D2"); } @Override Product getProduct() { System.out.println("完成产品ABCDTWO的创建"); return product; } }
指挥者,调用建造者对象中的部件构造与装配方法完成复杂对象的创建
public class Director {
//调用建造者对象中的部件构造与装配方法完成复杂对象的创建
public Product construct(Builder builder){
builder.buildA();
builder.buildB();
builder.buildC();
builder.buildD();
return builder.getProduct();
}
}
客户类,实现具体建造者1
public class Client {
public static void main(String[] args) {
Director director = new Director();
Product build = director.construct(new Worker());//实现具体建造者1
System.out.println(build.toString());
}
}
测试:
客户类,实现具体建造者2
public class Client {
public static void main(String[] args) {
Director director = new Director();
Product build = director.construct(new WorkerTwo());
System.out.println(build.toString());
}
}
测试:
上面示例是 Builder模式的常规用法,指挥者类 Director 在 Builder模式中具有很重要的作用,它用于指导具体构建者如何构建产品,控制调用先后次序,并向调用者返回完整的产品类,但是有些情况下需要简化系统结构,可以把Director和抽象建造者进行结合。
产品–>套餐
public class Product { //默认套餐 private String BuildA = "汉堡"; private String BuildB = "薯条"; private String BuildC = "炸鸡"; private String BuildD = "可乐"; public String getBuildA() { return BuildA; } public void setBuildA(String buildA) { BuildA = buildA; } public String getBuildB() { return BuildB; } public void setBuildB(String buildB) { BuildB = buildB; } public String getBuildC() { return BuildC; } public void setBuildC(String buildC) { BuildC = buildC; } public String getBuildD() { return BuildD; } public void setBuildD(String buildD) { BuildD = buildD; } @Override public String toString() { return "Product{" + "BuildA='" + BuildA + '\'' + ", BuildB='" + BuildB + '\'' + ", BuildC='" + BuildC + '\'' + ", BuildD='" + BuildD + '\'' + '}'; } }
抽象建造类
public abstract class Builder {
abstract Builder buildA(String msg);//汉堡
abstract Builder buildB(String msg);//薯条
abstract Builder buildC(String msg);//炸鸡
abstract Builder buildD(String msg);//可乐
//返回具体的套餐
abstract Product getProduct();
}
具体建造者–>服务员
public class Worker extends Builder{ private Product product; public Worker() { product = new Product(); } @Override Builder buildA(String msg) { product.setBuildA(msg); return this; } @Override Builder buildB(String msg) { product.setBuildB(msg); return this; } @Override Builder buildC(String msg) { product.setBuildC(msg); return this; } @Override Builder buildD(String msg) { product.setBuildD(msg); return this; } @Override Product getProduct() { return product; } }
客户:实现默认套餐
public class Client {
public static void main(String[] args) {
Worker worker = new Worker();
Product product = worker.getProduct();
System.out.println(product.toString());
}
}
测试:
实现自选套餐:
public class Client {
public static void main(String[] args) {
Worker worker = new Worker();
Product product = worker
.buildA("蛋糕")
.buildB("鸡腿")
.buildC("")
.buildD("")
.getProduct();
System.out.println(product.toString());
}
}
测试:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。