未來遊戲展:《戰國王朝》新宣傳片 展示封建時代的日本
建造者模式
簡介
什麼是建立型模式?
建立型模式(Creational Pattern)關注點是如何建立物件,其核心思想是要把物件的建立和使用相分離。這樣使得兩者能夠相對獨立地變換。
建立型模式對類的例項化過程進行了抽象,能夠將軟體模組中物件的建立和物件的使用分離。為了使軟體的結構更加清晰,外界對於這些物件只需要知道它們共同的介面,而不清楚器具體的實現和組合細節,使得整個系統的設計更加單一職責原則。
建立型模式在建立什麼(What),由誰建立(Who),何時建立(When)等方面都為軟體設計者提供了儘可能大的靈活性。
建立型模式隱藏了類的例項的建立細節,通過隱藏物件如何被建立和組合在一起達到整個系統獨立的目的。
什麼是建造者模式?
建造者模式(Builder Pattern)是常見設計模式的一種,它使用多個簡單的物件一步一步構建一個複雜的物件。這種型別的設計模式屬於建立型模式,它提供了一種建立物件的最佳方式
一個Builder類會一步步構造最終的物件,該Builder類是獨立於其他物件的。
典型應用
肯德基點餐,Java中的StringBuilder, lombook中的@Builder
使用場景
1.需要生成的物件具有複雜的內部結構
2.需要生成的物件內部屬性本身互相依賴
3.基本部件不變,而其組合經常變
優缺點對比
優點:1.建造者獨立,易擴充套件 2.便於控制細節風險
缺點:1.產品必須有共同點,範圍有控制 2.如果內部變化複雜,會有很多的建造類
實踐
個人感覺菜鳥上的示例挺好的,可以看下https://www.runoob.com/design-pattern/builder-pattern.html
以下為跟著狂神一起寫的例子
1.造房子示例
- 構建產品
package com.example.designPattern23.builder.demo0; /* 產品:房子 */ public class Product { public String buildA; public String buildB; public String buildC; public 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 + '\'' + '}'; } }
2.抽象builder
package com.example.designPattern23.builder.demo0;
public abstract class Builder {
abstract void buildA(); //地基
abstract void buildB(); //鋼筋工程
abstract void buildC(); //鋪電線
abstract void buildD(); //粉刷
abstract Product getProduct();
}
3.具體建造者
package com.example.designPattern23.builder.demo0;
public class Worker extends Builder {
public Product product;
public Worker() {
product = new Product();
}
@Override
void buildA() {
product.setBuildA("水泥");
}
@Override
void buildB() {
product.setBuildB("鋼筋工程");
}
@Override
void buildC() {
product.setBuildC("鋪電路");
}
@Override
void buildD() {
product.setBuildD("粉刷");
}
@Override
Product getProduct() {
return product;
}
}
4.指揮者
package com.example.designPattern23.builder.demo0;
// 指揮是核心。負責指揮構建一個工程。工程如何構建,由他確定
public class Director {
public Product build(Builder builder) {
builder.buildA();
builder.buildB();
builder.buildC();
builder.buildD();
return builder.getProduct();
}
}
5.測試類
package com.example.designPattern23.builder.demo0;
public class Test {
public static void main(String[] args) {
Director director = new Director();
Product product = director.build(new Worker());
System.out.println(product);
}
}
2.麥當勞點餐
與示例一的區別在於沒有指揮者,客戶為知會者
- 產品
package com.example.designPattern23.builder.demo1;
public class Product {
public String buildA = "可樂";
public String buildB = "雞翅";
public String buildC = "薯條";
public 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 + '\'' +
'}';
}
}
- 抽象建造者
package com.example.designPattern23.builder.demo1;
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();
}
- 具體建造者
package com.example.designPattern23.builder.demo1;
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;
}
}
- 測試類
package com.example.designPattern23.builder.demo1;
public class Test {
public static void main(String[] args) {
Worker worker = new Worker();
// System.out.println(worker.getProduct());
System.out.println(worker.buildA("全家桶").buildB("雞腿").getProduct());
}
}