Command pattern: Encapsulates a request into an object so that different requests can be parameterized by the client, queued to the request, or log the request logs, and support undoable operations.
Here is a demo of the command mode:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 __author__='Andy'5 """6 Liar design Mode7 design mode--Command mode8 command pattern: Encapsulates a request into an object so that different requests can be parameterized by the client, queued to the request, or log the request logs, and support undoable operations.9 """Ten One #Command Class A classCommand (object): - - def __init__(self, receiver): theSelf.receiver =receiver - - defExecute (self): - Pass + - #perform the actions of the receiver + classConcretecommand (Command): A at defExecute (self): - self.receiver.action () - - - #receive Command - classInvoker (object): in - def __init__(self): toSelf.command ="' + - defSetCommand (self,command): theSelf.command =Command * $ defExcutecommand (self):Panax Notoginseng Self.command.execute () - the + #Concrete Execution Class A classReceiver (object): the + defAction (self): - Print "Execute Request" $ $ if __name__=="__main__": - -R = Receiver ()#Create a command to execute thec = Concretecommand (R)#bind the command to execute -i = Invoker ()#Create a command performerWuyiI.setcommand (c)#Accept Commands theI.excutecommand ()#Execute Command
The design of the above class is as follows:
Advantages:
A command queue can be designed more easily
It is easier to log commands into the
Allow the party receiving the request to decide whether to veto the request
can easily implement revocation and redo of requests
Adding new specific command classes does not affect other classes, so it is easy to add new specific command classes
The biggest advantage of the command pattern is that it splits the object that requests an action with the object that knows how to perform an operation.
Andy
Source: http://www.cnblogs.com/onepiece-andy/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the author's consent must be in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Big talk design pattern Python implementation-Command mode