1、建造者模式介紹: 2、建造者模式角色分析 3、方式一:建造模式的常規用法(有指揮) 4、方式二:用戶可以自定義套餐和選擇預設套餐(沒有指揮者) 5、優點分析: 6、缺點分析: 7、應用場景: 8、建造者模式與抽象工廠模式的比較 ...
java設計模式3——建造者模式
1、建造者模式介紹:
建造者模式屬於創建型模式,他提供了一種創建對象得最佳方式
定義:
將一個複雜對象的構建和與它的表示分離,使得同樣的構建過程可以創建不同的表示
主要作用:
- 用戶不知道對象的建造過程和細節的情況下就可以直接創建複雜的對象
- 用戶只需要給出指定複雜對象的類型和內容,建造者模式負責按順序創建複雜的對象(把內部的建造過程和細節隱藏了起來)
舉例:
2、建造者模式角色分析
3、方式一:建造模式的常規用法(有指揮)
3.1、構建抽象的建造者(圖紙)
package com.xgp.company.第三種_建造者模式.常規用法_套餐用法;
/**
* 抽象的建造者
*/
public abstract class Builder {
abstract void buildA(); //地基
abstract void buildB(); //鋼筋水泥
abstract void buildC(); //鋪電線
abstract void buildD(); //粉刷
//完工:等到產品
abstract Product getProduct();
}
3.2、定義產品(房子)
package com.xgp.company.第三種_建造者模式.常規用法_套餐用法;
//產品:房子
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) {
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 + '\'' +
'}';
}
}
3.3、定義具體的建造者(工人)
package com.xgp.company.第三種_建造者模式.常規用法_套餐用法;
//具體的建造者
public class Worker extends Builder {
private Product product;
public Worker() {
product = new Product();
}
@Override
void buildA() {
product.setBuildA("鋪地基");
System.out.println("鋪地基");
}
@Override
void buildB() {
product.setBuildB("搭鋼筋");
System.out.println("搭鋼筋");
}
@Override
void buildC() {
product.setBuildC("鋪電線");
System.out.println("鋪電線");
}
@Override
void buildD() {
product.setBuildD("粉刷牆壁");
System.out.println("粉刷牆壁");
}
@Override
Product getProduct() {
return product;
}
}
3.4、定義指揮者,指揮工人建造
package com.xgp.company.第三種_建造者模式.常規用法_套餐用法;
/**
* 指揮:核心,負責指揮構建一個工程,工程如何構建,由他來決定
*/
public class Director {
//指揮工人創建房子
public Product build(Builder builder) {
//創建產品
builder.buildA();
builder.buildB();
builder.buildC();
builder.buildD();
return builder.getProduct();
}
}
3.5、創建測試類,進行建造
package com.xgp.company.第三種_建造者模式.常規用法_套餐用法;
public class Test {
public static void main(String[] args) {
//指揮
Director director = new Director();
//指揮具體的工人完成創建
Product build = director.build(new Worker());
System.out.println(build.toString());
}
}
結果:
鋪地基
搭鋼筋
鋪電線
粉刷牆壁
Product{buildA='鋪地基', buildB='搭鋼筋', buildC='鋪電線', buildD='粉刷牆壁'}
4、方式二:用戶可以自定義套餐和選擇預設套餐(沒有指揮者)
4.1、定義抽象的建造者
package com.xgp.company.第三種_建造者模式.通過靜態內部類來實現;
/**
* 抽象的建造者
*/
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();
}
4.2、定義產品和預設套餐
package com.xgp.company.第三種_建造者模式.通過靜態內部類來實現;
/**
* 套餐
*/
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) {
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 + '\'' +
'}';
}
}
4.3、定義具體的建造者(服務員)
package com.xgp.company.第三種_建造者模式.通過靜態內部類來實現;
//具體的建造者
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;
}
}
4.4、定義顧客,進行點餐
package com.xgp.company.第三種_建造者模式.通過靜態內部類來實現;
public class Test {
public static void main(String[] args) {
//服務員
Worker worker = new Worker();
Product product = worker.getProduct();
System.out.println(product.toString());
//自定義套餐
product = worker.buildA("aaa").buildB("bbb").buildC("cccc").buildD("ddd").getProduct();
System.out.println(product.toString());
}
}
運行結果:
Product{buildA='漢堡', buildB='可樂', buildC='薯條', buildD='甜點'}
Product{buildA='aaa', buildB='bbb', buildC='cccc', buildD='ddd'}