Big talk design pattern Python implementation-Command mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.