实现命令模式示例,添加接收者、命令及传令官类,增强代码可读性
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
实例:
|
||||
汤宝宝成为一国之王,手下有一个传令官和三个大臣,分别是内政大臣、外交大臣和军事大臣。
|
||||
有一天,汤宝宝想要发布一个命令,传令官负责将命令传达给大臣们,大臣们根据命令的内容决定是否执行。
|
||||
*/
|
||||
|
||||
// 接收者接口
|
||||
interface Receiver {
|
||||
perform(): void;
|
||||
abort(): void;
|
||||
}
|
||||
|
||||
// 具体接收者:内政大臣
|
||||
class HomeSecretary implements Receiver {
|
||||
isPerforming: boolean;
|
||||
constructor() {
|
||||
this.isPerforming = false;
|
||||
}
|
||||
|
||||
perform(): void {
|
||||
this.isPerforming = true;
|
||||
console.log("内政大臣开始执行命令");
|
||||
}
|
||||
|
||||
abort(): void {
|
||||
this.isPerforming = false;
|
||||
console.log("内政大臣取消执行命令");
|
||||
}
|
||||
}
|
||||
|
||||
// 具体接收者:外交大臣
|
||||
class ForeignSecretary implements Receiver {
|
||||
isPerforming: boolean;
|
||||
constructor() {
|
||||
this.isPerforming = false;
|
||||
}
|
||||
|
||||
perform(): void {
|
||||
this.isPerforming = true;
|
||||
console.log("外交大臣开始执行命令");
|
||||
}
|
||||
|
||||
abort(): void {
|
||||
this.isPerforming = false;
|
||||
console.log("外交大臣取消执行命令");
|
||||
}
|
||||
}
|
||||
|
||||
// 具体接收者:军事大臣
|
||||
class MilitarySecretary implements Receiver {
|
||||
isPerforming: boolean;
|
||||
constructor() {
|
||||
this.isPerforming = false;
|
||||
}
|
||||
|
||||
perform(): void {
|
||||
this.isPerforming = true;
|
||||
console.log("军事大臣开始执行命令");
|
||||
}
|
||||
|
||||
abort(): void {
|
||||
this.isPerforming = false;
|
||||
console.log("军事大臣取消执行命令");
|
||||
}
|
||||
}
|
||||
|
||||
// 命令接口
|
||||
interface Command {
|
||||
execute(): void;
|
||||
}
|
||||
|
||||
// 执行命令
|
||||
class PerformCommand implements Command {
|
||||
receiver: Receiver;
|
||||
|
||||
constructor(receiver: Receiver) {
|
||||
this.receiver = receiver;
|
||||
}
|
||||
|
||||
execute(): void {
|
||||
this.receiver.perform();
|
||||
}
|
||||
}
|
||||
|
||||
// 取消命令
|
||||
class AbortCommand implements Command {
|
||||
receiver: Receiver;
|
||||
|
||||
constructor(receiver: Receiver) {
|
||||
this.receiver = receiver;
|
||||
}
|
||||
execute(): void {
|
||||
this.receiver.abort();
|
||||
}
|
||||
}
|
||||
|
||||
// 传令官
|
||||
class Herald {
|
||||
private commands: Command[] = [];
|
||||
|
||||
addCommand(command: Command): void {
|
||||
this.commands.push(command);
|
||||
}
|
||||
executeCommands(): void {
|
||||
for (const command of this.commands) {
|
||||
command.execute();
|
||||
}
|
||||
this.commands = []; // 执行完后清空命令列表
|
||||
}
|
||||
}
|
||||
|
||||
(function () {
|
||||
console.log("======= 命令模式开始 =======");
|
||||
|
||||
// 创建接收者
|
||||
const homeSecretary = new HomeSecretary();
|
||||
const foreignSecretary = new ForeignSecretary();
|
||||
const defenseSecretary = new MilitarySecretary();
|
||||
|
||||
// 创建命令
|
||||
const performHomeCommand = new PerformCommand(homeSecretary);
|
||||
const abortHomeCommand = new AbortCommand(homeSecretary);
|
||||
const performForeignCommand = new PerformCommand(foreignSecretary);
|
||||
const abortForeignCommand = new AbortCommand(foreignSecretary);
|
||||
const performDefenseCommand = new PerformCommand(defenseSecretary);
|
||||
const abortDefenseCommand = new AbortCommand(defenseSecretary);
|
||||
|
||||
// 创建传令官
|
||||
const herald = new Herald();
|
||||
|
||||
// 添加命令
|
||||
herald.addCommand(performHomeCommand);
|
||||
herald.addCommand(performForeignCommand);
|
||||
herald.addCommand(performDefenseCommand);
|
||||
|
||||
// 执行命令
|
||||
herald.executeCommands();
|
||||
|
||||
// 取消命令
|
||||
herald.addCommand(abortHomeCommand);
|
||||
herald.addCommand(abortForeignCommand);
|
||||
herald.addCommand(abortDefenseCommand);
|
||||
|
||||
// 执行取消命令
|
||||
herald.executeCommands();
|
||||
|
||||
console.log("\n======= 命令模式结束 =======");
|
||||
})();
|
||||
Reference in New Issue
Block a user