设计模式之建造者模式
时间:2022-05-27 23:15:02
收藏:0
阅读:18
建造者模式
-
建造者模式典型应用场景:包工头安排工人建一桌房子,产品需要的材料 地基,钢铁,材料
-
代码实现关键点 包工头对象、工人对象、产品对象、建造者对象:
-
有包工头指挥工人的实现代码如下
- 建造者对象,包含材料及实现
package com.gof23.builder1.demo1;
public abstract class Builder {
public abstract void builderA();
public abstract void builderB();
public abstract void builderC();
public abstract Room getRoom();
}
- 房子对象
package com.gof23.builder1.demo1;
public class Room {
private String builderA ;
public String getBuilderA() {
return builderA;
}
public void setBuilderA(String builderA) {
this.builderA = builderA;
}
public String getBuilderB() {
return builderB;
}
public void setBuilderB(String builderB) {
this.builderB = builderB;
}
public String getBuilderC() {
return builderC;
}
public void setBuilderC(String builderC) {
this.builderC = builderC;
}
private String builderB;
private String builderC;
@Override
public String toString() {
return "Room{" +
"builderA=‘" + builderA + ‘\‘‘ +
", builderB=‘" + builderB + ‘\‘‘ +
", builderC=‘" + builderC + ‘\‘‘ +
‘}‘;
}
}
- 工人对象
package com.gof23.builder1.demo1;
public class Worker extends Builder {
private Room room;
public Worker(){
this.room = new Room();
}
@Override
public void builderA() {
this.room.setBuilderA("A");
}
@Override
public void builderB() {
this.room.setBuilderB("M");
}
@Override
public void builderC() {
this.room.setBuilderC("C");
}
public Room getRoom() {
return room;
}
}
- 包工头对象
package com.gof23.builder1.demo1;
public class Directory {
public Room builder(Builder builder){
builder.builderB();
builder.builderA();
builder.builderC();
return builder.getRoom();
}
}
- client测试实现包工头指挥工人造房子
package com.gof23.builder1.demo1;
public class Client {
public static void main(String[] args) {
Directory directory = new Directory();
Room room = directory.builder(new Worker());
System.out.println(room.toString());
}
}
- 无包工头,老板或者客户直接指挥工人造房子实现代码如下
- 建造者对象,包含材料及实现
package com.gof23.builder1.demo1;
public abstract class Builder {
public abstract Builder builderA(String msg);
public abstract Builder builderB(String msg);
public abstract Builder builderC(String msg);
public abstract Room getRoom();
}
- 房子对象
package com.gof23.builder1.demo1;
public class Room {
private String builderA ;
public String getBuilderA() {
return builderA;
}
public void setBuilderA(String builderA) {
this.builderA = builderA;
}
public String getBuilderB() {
return builderB;
}
public void setBuilderB(String builderB) {
this.builderB = builderB;
}
public String getBuilderC() {
return builderC;
}
public void setBuilderC(String builderC) {
this.builderC = builderC;
}
private String builderB;
private String builderC;
@Override
public String toString() {
return "Room{" +
"builderA=‘" + builderA + ‘\‘‘ +
", builderB=‘" + builderB + ‘\‘‘ +
", builderC=‘" + builderC + ‘\‘‘ +
‘}‘;
}
}
- 工人对象
package com.gof23.builder1.demo2;
public class Worker extends Builder {
private Room room;
public Worker(){
this.room = new Room();
}
@Override
public Builder builderA(String msg) {
this.room.setBuilderA(msg);
return this;
}
@Override
public Builder builderB(String msg) {
this.room.setBuilderB(msg);
return this;
}
@Override
public Builder builderC(String msg) {
this.room.setBuilderC(msg);
return this;
}
public Room getRoom() {
return room;
}
}
- client测试模拟实现直接指挥工人造房子,这种方式更灵活,建房子的材料及步骤都可控制
package com.gof23.builder1.demo2;
public class Client {
public static void main(String[] args) {
Room room = new Worker().builderA("水泥地").builderC("MMM").builderB("sdfsdfsdf").getRoom();
System.out.println(room.toString());
}
}
小结: 1. 建造者模式应用比较框,底层框架用的也比较多
2. 实现指挥者的建造者模式相对无实现指挥者的建造者模式更笨拙点,只能由具体的指挥者或包工头来定义建房子的材料及步骤,而无实现具体指挥者的,client测试模拟实现直接指挥工人造房子,这种方式更灵活,建房子的材料及步骤都可控制
推荐方式2
原文:https://www.cnblogs.com/lwdesire/p/15350330.html
评论(0)