更新生成者模式示例,添加木屋和砖房建造者实现,重构相关代码并增强可读性

This commit is contained in:
2025-12-09 21:55:32 +08:00
parent 29e554b670
commit cc736d524b
7 changed files with 394 additions and 21 deletions
+9 -9
View File
@@ -1,6 +1,7 @@
/*
实例
今天伟哥约自己在富士康工作的老乡出来喝酒,没想到老乡最近一直在加班。听说接了个大活,要同时为小米公司和苹果公司代工他们的笔记本电脑和手机生产业务。
今天汤宝宝约自己在富士康工作的老乡出来喝酒,没想到老乡最近一直在加班。
听说接了个大活,要同时为小米公司和苹果公司代工他们的笔记本电脑和手机生产业务。
*/
// 抽象产品 - 手机
@@ -76,6 +77,7 @@ class XiaomiFactory extends AbstractFactory {
}
}
// 客户端代码
const client = (factory: AbstractFactory) => {
const phone = factory.createPhone();
const laptop = factory.createLaptop();
@@ -85,22 +87,20 @@ const client = (factory: AbstractFactory) => {
laptop.setOS();
};
function main() {
(function () {
console.log("======== 抽象工厂模式 ========");
// 伟哥先去苹果工厂生产产品
// 先去苹果工厂生产产品
let factory: AbstractFactory = new AppleFactory();
console.log(`伟哥选择了${factory.name}生产产品:`);
console.log(`汤宝宝选择了${factory.name}生产产品:`);
client(factory);
console.log("\n切换工厂生产其他品牌的产品...\n");
// 伟哥又去小米工厂生产产品
// 又去小米工厂生产产品
factory = new XiaomiFactory();
console.log(`伟哥选择了${factory.name}生产产品:`);
console.log(`汤宝宝选择了${factory.name}生产产品:`);
client(factory);
console.log("====== 抽象工厂模式结束 ======");
}
main();
})();
+131
View File
@@ -0,0 +1,131 @@
/*
实例
今天汤宝宝约自己在富士康工作的老乡出来喝酒,没想到老乡最近一直在加班。
听说接了个大活,要同时为小米公司和苹果公司代工他们的笔记本电脑和手机生产业务。
*/
// 产品: 房子
class House {
foundation: string | undefined;
structure: string | undefined;
roof: string | undefined;
}
// 抽象建造者: 房屋建造者
abstract class HouseBuilder {
abstract house: House;
abstract buildFoundation(): void;
abstract buildStructure(): void;
abstract buildRoof(): void;
abstract getHouse(): House;
}
class WoodenHouseBuilder extends HouseBuilder {
house: House;
constructor() {
super();
this.house = new House();
}
buildFoundation(): void {
this.house.foundation = "木桩地基";
console.log(" 建造木桩地基");
}
buildStructure(): void {
this.house.structure = "木结构";
console.log(" 建造木结构");
}
buildRoof(): void {
this.house.roof = "木屋顶";
console.log(" 建造木屋顶");
}
getHouse(): House {
return this.house;
}
}
class BrickHouseBuilder extends HouseBuilder {
house: House;
constructor() {
super();
this.house = new House();
}
buildFoundation(): void {
this.house.foundation = "混凝土地基";
console.log(" 建造混凝土地基");
}
buildStructure(): void {
this.house.structure = "砖结构";
console.log(" 建造砖结构");
}
buildRoof(): void {
this.house.roof = "瓦屋顶";
console.log(" 建造瓦屋顶");
}
getHouse(): House {
return this.house;
}
}
// 指挥者
class Director {
private builder: HouseBuilder;
public house: House;
constructor() {
this.builder = {} as HouseBuilder;
this.house = new House();
}
public reset(): void {
this.house = new House();
}
public setBuilder(builder: HouseBuilder): void {
this.builder = builder;
}
public constructHouse(): void {
this.builder.buildFoundation();
this.builder.buildStructure();
this.builder.buildRoof();
this.house = this.builder.getHouse();
}
}
(function () {
console.log("======== 建造者模式 ========");
const director = new Director();
console.log("建造木屋:");
let builder = new WoodenHouseBuilder();
director.setBuilder(builder);
director.constructHouse();
let house = director.house;
console.log(
`木屋建造完成:`,
house.foundation,
house.structure,
house.roof,
"\n"
);
console.log("建造砖房:");
builder = new BrickHouseBuilder();
director.setBuilder(builder);
director.constructHouse();
house = director.house;
console.log(
`砖房建造完成:`,
house.foundation,
house.structure,
house.roof,
"\n"
);
console.log("====== 生成者模式结束 ======");
})();