Python: Command Pattern
GeovinDuCommand.py
# 命令模式 Command Pattern GeovinDuCommand.py # Web Browser: class WebBrowser(object): def __init__(self): self.bookmarks = [] self.currentUrl = None def Navigate(self, url): self.currentUrl = url def BookmarkCurrentPage(self): valid = self.currentUrl is not None contains = self.currentUrl in self.bookmarks if valid and not contains: self.bookmarks.append(self.currentUrl) def RemoveBookmark(self, url): self.bookmarks.remove(url) def PrintBookmarks(self): print("书签:") for i in self.bookmarks: print("\t" + str(i)) # AddBookmarkCommand: class AddBookmarkCommand(object): def __init__(self, webBrowser): self.webBrowser = webBrowser self.url = None def Execute(self): self.url = self.webBrowser.currentUrl self.webBrowser.BookmarkCurrentPage() def Undo(self): self.webBrowser.RemoveBookmark(self.url) def Redo(self): self.Execute() # Bookmarker: class Bookmarker(object): def __init__(self, webBrowser): self.webBrowser = webBrowser self.undoStack = [] self.redoStack = [] def BookmarkCurrentPage(self): command = AddBookmarkCommand(self.webBrowser) self.undoStack.append(command) self.redoStack = [] command.Execute() def UndoBookmark(self): if len(self.undoStack) != 0: command = self.undoStack.pop() self.redoStack.append(command) command.Undo() def RedoBookmark(self): if len(self.redoStack) != 0: command = self.redoStack.pop() self.undoStack.append(command) command.Redo() # 2 geovindu class data: _data = 0 def __init__(self, value=0) -> None: data._data += value def add(self): pass def subtract(self): pass def __str__(self) -> str: return f"现值: {data._data}" class Addition(data): def __init__(self, value=0) -> None: super().__init__(value) def add(self, value): data._data += value print("正在执行加法操作...") def __str__(self) -> str: return super().__str__() class Subtraction(data): def __init__(self, value=0) -> None: super().__init__(value) def subtract(self, value): data._data -= value print("正在进行减法运算...") def __str__(self) -> str: return super().__str__() class Command(): def execute(self): pass def undo(self): pass class AdditionCommand(Command): def __init__(self, obj, value): self.obj = obj self.value = value def execute(self): self.obj.add(self.value) def undo(self, commandObj): commandObj.set_command(SubtractionCommand(Subtraction(), self.value)) commandObj.invoke() class SubtractionCommand(Command): def __init__(self, obj, value): self.obj = obj self.value = value def execute(self): self.obj.subtract(self.value) def undo(self, commandObj): commandObj.set_command(AdditionCommand(Addition(), self.value)) commandObj.invoke() class ActionInvoker: def __init__(self, command): self.command = command def set_command(self, command): self.command = command def invoke(self): self.command.execute() def undo(self): print("[!UNDO] ", end="") self.command.undo(self)
main.py 调用
# 命令模式 Command Pattern memeSurfer = GeovinDuCommand.WebBrowser() bookmarker = GeovinDuCommand.Bookmarker(memeSurfer) memeSurfer.Navigate("geovindu.gov") bookmarker.BookmarkCurrentPage() memeSurfer.PrintBookmarks() memeSurfer.Navigate("dusystem.com") bookmarker.BookmarkCurrentPage() memeSurfer.PrintBookmarks() bookmarker.UndoBookmark() memeSurfer.PrintBookmarks() bookmarker.RedoBookmark() memeSurfer.PrintBookmarks() print("\n") obj = GeovinDuCommand.data(100) print(obj) command_addition = GeovinDuCommand.AdditionCommand(GeovinDuCommand.Addition(), 10) # concrete command command_subtraction = GeovinDuCommand.SubtractionCommand(GeovinDuCommand.Subtraction(), 10) # concrete command action_invoker = GeovinDuCommand.ActionInvoker(command_addition); # invoker action_invoker.invoke() print(obj) action_invoker.set_command(command_subtraction) action_invoker.invoke() print(obj) action_invoker.undo() print(obj) action_invoker.undo() print(obj)
输出:
书签: geovindu.gov 书签: geovindu.gov dusystem.com 书签: geovindu.gov 书签: geovindu.gov dusystem.com 现值: 100 正在执行加法操作... 现值: 110 正在进行减法运算... 现值: 100 [!UNDO] 正在执行加法操作... 现值: 110 [!UNDO] 正在进行减法运算... 现值: 100 Process finished with exit code 0
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)