Python: Command Pattern
GeovinDuCommand.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | # 命令模式 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 调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # 命令模式 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) |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 书签: 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(涂聚文)
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2016-10-27 Concurrency in csharp (Asynchronous, Parallel, and Multithreaded Programming)
2015-10-27 csharp: InvokeHelper
2015-10-27 Free Slideshow, Gallery And Lightboxes Scripts
2015-10-27 35 Gallery – Ajax Slide