更新各个设计模式示例,添加工厂方法、原型和单例模式的实现,重构相关代码并增强可读性

This commit is contained in:
2025-12-11 16:57:10 +08:00
parent cc736d524b
commit 357c4748f8
10 changed files with 558 additions and 1 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ class Director {
}
(function () {
console.log("======== 建造者模式 ========");
console.log("======== 生成者模式 ========");
const director = new Director();
+59
View File
@@ -0,0 +1,59 @@
/*
实例
小汤有一家披萨店,他想通过工厂方法模式来创建不同类型的披萨。
首先,他定义了一个披萨接口和具体的披萨类,然后创建了一个披萨工厂接口和具体的披萨工厂类来生产不同类型的披萨。
*/
// 披萨接口
interface Pizza {
show(): void;
}
// 披萨工厂接口
interface PizzaFactory {
createPizza(): Pizza;
}
// 具体的披萨类
class CheesePizza implements Pizza {
show(): void {
console.log(" 这是个芝士披萨!\n");
}
}
class PepperoniPizza implements Pizza {
show(): void {
console.log(" 这是个意大利辣味披萨!\n");
}
}
// 具体的披萨工厂类
class CheesePizzaFactory implements PizzaFactory {
createPizza(): Pizza {
return new CheesePizza();
}
}
class PepperoniPizzaFactory implements PizzaFactory {
createPizza(): Pizza {
return new PepperoniPizza();
}
}
(function () {
console.log("========= 工厂方法模式 =========");
// 创建芝士披萨
let factory: PizzaFactory = new CheesePizzaFactory();
console.log("芝士披萨工厂启动:");
let pizza: Pizza = factory.createPizza();
pizza.show();
// 创建意大利辣味披萨
factory = new PepperoniPizzaFactory();
console.log("意大利辣味披萨工厂启动:");
pizza = factory.createPizza();
pizza.show();
console.log("======= 工厂方法模式结束 =======");
})();
+81
View File
@@ -0,0 +1,81 @@
/*
实例
汤宝宝穿越到了星球大战世界,并掉到了克隆人军队的生产工厂。
ps:
ts中没有官方的深拷贝实现,lodash中有deepclone函数以深拷贝对象,但性能较差。
object.assign和扩展运算符只能实现浅拷贝,JSON.parse(JSON.stringify(obj))虽然能实现深拷贝,但会丢失函数、undefined、symbol等特殊值,且无法处理循环引用的对象。
比较安全的方法是逐步复制每个字段,会有稍许麻烦。
*/
// 克隆人接口
interface CloneTrooper {
clone(): CloneTrooper;
fight(): void;
setid(id: number): void;
}
// 具体的克隆人类
class CloneTrooperA implements CloneTrooper {
private id: number;
constructor(id: number) {
this.id = id;
}
clone(): CloneTrooper {
console.log(`克隆人A-${this.id}号正在克隆...`);
return new CloneTrooperA(this.id);
}
fight(): void {
console.log(`克隆人A-${this.id}号准备战斗!`);
}
setid(id: number): void {
this.id = id;
}
}
class CloneTrooperB implements CloneTrooper {
private id: number;
constructor(id: number) {
this.id = id;
}
clone(): CloneTrooper {
console.log(`克隆人B-${this.id}号正在克隆...`);
return new CloneTrooperB(this.id);
}
fight(): void {
console.log(`克隆人B-${this.id}号准备战斗!`);
}
setid(id: number): void {
this.id = id;
}
}
(function () {
console.log("========= 原型模式 =========");
// 创建克隆人A原型
const prototypeA: CloneTrooper = new CloneTrooperA(0);
prototypeA.fight();
const cloneA1 = prototypeA.clone();
cloneA1.setid(1);
cloneA1.fight();
const cloneA2 = cloneA1.clone();
cloneA2.setid(2);
cloneA2.fight();
// 创建克隆人B原型
const prototypeB: CloneTrooper = new CloneTrooperB(0);
prototypeB.fight();
const cloneB1 = prototypeB.clone();
cloneB1.setid(1);
cloneB1.fight();
const cloneB2 = cloneB1.clone();
cloneB2.setid(2);
cloneB2.fight();
console.log("\n======= 原型模式结束 =======");
})();
+34
View File
@@ -0,0 +1,34 @@
/*
实例
汤宝宝混穿到了泰拉世界,并成为了泰拉联邦的唯一统治者。人类只有一个帝皇!
*/
class Emperor {
private static instance: Emperor;
readonly name: string;
static getInstance(name: string, announce: string): Emperor {
console.log(announce);
if (!this.instance) {
this.instance = new Emperor(name, announce);
}
return this.instance;
}
private constructor(name: string, announce: string) {
this.name = name;
}
}
(function () {
console.log("========= 单例模式 =========");
const emperor1 = Emperor.getInstance("汤宝宝", "泰拉联邦的帝皇诞生了!");
console.log(`帝皇的名字是:${emperor1.name}!`);
const emperor2 = Emperor.getInstance("王二狗", "泰拉联邦的帝皇又一次诞生了!");
console.log(`帝皇的名字是:${emperor2.name}!`);
if (emperor1 === emperor2) {
console.log(`泰拉只有一个帝皇:${emperor1.name}!\n`);
}
console.log("======= 单例模式结束 =======");
})();