实现命令模式示例,添加接收者、命令及传令官类,增强代码可读性
This commit is contained in:
parent
feaa733595
commit
3a23a4d95a
|
|
@ -0,0 +1,137 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
/*
|
||||||
|
实例:
|
||||||
|
汤宝宝成为一国之王,手下有一个传令官和三个大臣,分别是内政大臣、外交大臣和军事大臣。
|
||||||
|
有一天,汤宝宝想要发布一个命令,传令官负责将命令传达给大臣们,大臣们根据命令的内容决定是否执行。
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 接收者接口
|
||||||
|
type Receiver interface {
|
||||||
|
perform()
|
||||||
|
abort()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 内政大臣
|
||||||
|
type HomeSecretary struct {
|
||||||
|
isPerforming bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HomeSecretary) perform() {
|
||||||
|
h.isPerforming = true
|
||||||
|
fmt.Println("内政大臣开始执行命令")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HomeSecretary) abort() {
|
||||||
|
h.isPerforming = false
|
||||||
|
fmt.Println("内政大臣取消执行命令")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 外交大臣
|
||||||
|
type ForeignSecretary struct {
|
||||||
|
isPerforming bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *ForeignSecretary) perform() {
|
||||||
|
f.isPerforming = true
|
||||||
|
fmt.Println("外交大臣开始执行命令")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *ForeignSecretary) abort() {
|
||||||
|
f.isPerforming = false
|
||||||
|
fmt.Println("外交大臣取消执行命令")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 军事大臣
|
||||||
|
type DefenseSecretary struct {
|
||||||
|
isPerforming bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DefenseSecretary) perform() {
|
||||||
|
d.isPerforming = true
|
||||||
|
fmt.Println("军事大臣开始执行命令")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DefenseSecretary) abort() {
|
||||||
|
d.isPerforming = false
|
||||||
|
fmt.Println("军事大臣取消执行命令")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 命令接口
|
||||||
|
type Command interface {
|
||||||
|
execute()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行命令
|
||||||
|
type PerformCommand struct {
|
||||||
|
receiver Receiver
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PerformCommand) execute() {
|
||||||
|
c.receiver.perform()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 取消命令
|
||||||
|
type AbortCommand struct {
|
||||||
|
receiver Receiver
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *AbortCommand) execute() {
|
||||||
|
c.receiver.abort()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invoker - 传令官
|
||||||
|
type Herald struct {
|
||||||
|
commands []Command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Herald) addCommand(command Command) {
|
||||||
|
h.commands = append(h.commands, command)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Herald) executeCommands() {
|
||||||
|
for _, command := range h.commands {
|
||||||
|
command.execute()
|
||||||
|
}
|
||||||
|
h.commands = []Command{} // 清空命令列表
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("========= 命令模式 =========")
|
||||||
|
// 创建接收者
|
||||||
|
homeSecretary := &HomeSecretary{}
|
||||||
|
foreignSecretary := &ForeignSecretary{}
|
||||||
|
defenseSecretary := &DefenseSecretary{}
|
||||||
|
|
||||||
|
// 创建命令
|
||||||
|
performHomeCommand := &PerformCommand{receiver: homeSecretary}
|
||||||
|
abortHomeCommand := &AbortCommand{receiver: homeSecretary}
|
||||||
|
performForeignCommand := &PerformCommand{receiver: foreignSecretary}
|
||||||
|
abortForeignCommand := &AbortCommand{receiver: foreignSecretary}
|
||||||
|
performDefenseCommand := &PerformCommand{receiver: defenseSecretary}
|
||||||
|
abortDefenseCommand := &AbortCommand{receiver: defenseSecretary}
|
||||||
|
|
||||||
|
// 创建传令官
|
||||||
|
herald := &Herald{}
|
||||||
|
|
||||||
|
// 添加命令
|
||||||
|
herald.addCommand(performHomeCommand)
|
||||||
|
herald.addCommand(performForeignCommand)
|
||||||
|
herald.addCommand(performDefenseCommand)
|
||||||
|
|
||||||
|
// 执行命令
|
||||||
|
herald.executeCommands()
|
||||||
|
|
||||||
|
// 取消命令
|
||||||
|
herald.addCommand(abortHomeCommand)
|
||||||
|
herald.addCommand(abortForeignCommand)
|
||||||
|
herald.addCommand(abortDefenseCommand)
|
||||||
|
|
||||||
|
// 执行取消命令
|
||||||
|
herald.executeCommands()
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println("======= 命令模式结束 =======")
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,147 @@
|
||||||
|
"""
|
||||||
|
实例:
|
||||||
|
汤宝宝成为一国之王,手下有一个传令官和三个大臣,分别是内政大臣、外交大臣和军事大臣。
|
||||||
|
有一天,汤宝宝想要发布一个命令,传令官负责将命令传达给大臣们,大臣们根据命令的内容决定是否执行。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
# 接收者接口
|
||||||
|
class Receiver(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def perform(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def abort(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# 内政大臣
|
||||||
|
class HomeSecretary(Receiver):
|
||||||
|
is_performing: bool
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.is_performing = False
|
||||||
|
|
||||||
|
def perform(self):
|
||||||
|
self.is_performing = True
|
||||||
|
print("内政大臣开始执行命令")
|
||||||
|
|
||||||
|
def abort(self):
|
||||||
|
self.is_performing = False
|
||||||
|
print("内政大臣取消执行命令")
|
||||||
|
|
||||||
|
|
||||||
|
# 外交大臣
|
||||||
|
class ForeignSecretary(Receiver):
|
||||||
|
is_performing: bool
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.is_performing = False
|
||||||
|
|
||||||
|
def perform(self):
|
||||||
|
self.is_performing = True
|
||||||
|
print("外交大臣开始执行命令")
|
||||||
|
|
||||||
|
def abort(self):
|
||||||
|
self.is_performing = False
|
||||||
|
print("外交大臣取消执行命令")
|
||||||
|
|
||||||
|
|
||||||
|
# 军事大臣
|
||||||
|
class DefenseSecretary(Receiver):
|
||||||
|
is_performing: bool
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.is_performing = False
|
||||||
|
|
||||||
|
def perform(self):
|
||||||
|
self.is_performing = True
|
||||||
|
print("军事大臣开始执行命令")
|
||||||
|
|
||||||
|
def abort(self):
|
||||||
|
self.is_performing = False
|
||||||
|
print("军事大臣取消执行命令")
|
||||||
|
|
||||||
|
|
||||||
|
# 命令接口
|
||||||
|
class Command(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def execute(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# 执行命令
|
||||||
|
class performCommand(Command):
|
||||||
|
receiver: Receiver
|
||||||
|
|
||||||
|
def __init__(self, receiver: Receiver):
|
||||||
|
self.receiver = receiver
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
self.receiver.perform()
|
||||||
|
|
||||||
|
|
||||||
|
# 取消命令
|
||||||
|
class abortCommand(Command):
|
||||||
|
receiver: Receiver
|
||||||
|
|
||||||
|
def __init__(self, receiver: Receiver):
|
||||||
|
self.receiver = receiver
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
self.receiver.abort()
|
||||||
|
|
||||||
|
|
||||||
|
# 传令官
|
||||||
|
class Herald:
|
||||||
|
commands: list[Command]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.commands = []
|
||||||
|
|
||||||
|
def addCommand(self, command: Command):
|
||||||
|
self.commands.append(command)
|
||||||
|
|
||||||
|
def executeCommands(self):
|
||||||
|
for command in self.commands:
|
||||||
|
command.execute()
|
||||||
|
self.commands.clear()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("========= 命令模式 =========")
|
||||||
|
# 创建接收者
|
||||||
|
home_secretary = HomeSecretary()
|
||||||
|
foreign_secretary = ForeignSecretary()
|
||||||
|
defense_secretary = DefenseSecretary()
|
||||||
|
|
||||||
|
# 创建命令
|
||||||
|
perform_home_command = performCommand(home_secretary)
|
||||||
|
perform_foreign_command = performCommand(foreign_secretary)
|
||||||
|
perform_defense_command = performCommand(defense_secretary)
|
||||||
|
|
||||||
|
abort_home_command = abortCommand(home_secretary)
|
||||||
|
abort_foreign_command = abortCommand(foreign_secretary)
|
||||||
|
abort_defense_command = abortCommand(defense_secretary)
|
||||||
|
|
||||||
|
# 创建传令官
|
||||||
|
herald = Herald()
|
||||||
|
|
||||||
|
# 添加执行命令
|
||||||
|
herald.addCommand(perform_home_command)
|
||||||
|
herald.addCommand(perform_foreign_command)
|
||||||
|
herald.addCommand(perform_defense_command)
|
||||||
|
|
||||||
|
# 执行命令
|
||||||
|
herald.executeCommands()
|
||||||
|
|
||||||
|
# 添加取消命令
|
||||||
|
herald.addCommand(abort_home_command)
|
||||||
|
herald.addCommand(abort_foreign_command)
|
||||||
|
herald.addCommand(abort_defense_command)
|
||||||
|
|
||||||
|
# 执行取消命令
|
||||||
|
herald.executeCommands()
|
||||||
|
|
||||||
|
print("\n======= 命令模式结束 =======")
|
||||||
|
|
@ -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======= 命令模式结束 =======");
|
||||||
|
})();
|
||||||
Loading…
Reference in New Issue