Command mode (Python)

Source: Internet
Author: User

Command mode: Encapsulate a request as an object so that you can parameterize the customer with different requests. It supports request queuing, request logging, and unrecoverable operations.

Advantages: Separates the objects that request an operation from those that know how to perform an operation.

# Encoding = UTF-8 ## by Panda # command mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') import time # commandclass command (): Cycler = none def _ init _ (self, Cycler): Self. runner ER = runner er def execute (Self): pass # specific command class: copy a mutton string class bakemuttoncommand (command): def sethandsetsoft (self, runner ER): Command. _ init _ (self, runner ER) def execute (Self): Self. aggreger. bakemutton () def tostring (Self): Return 'catalogue string' # command class: Copy chicken wings class bakechickenwingcommand (command): def sethandsetsoft (self, Handler ER): Command. _ init _ (self, runner ER) def execute (Self): Self. aggreger. bakechickenwing () def tostring (Self): Return 'Copy chicken wings '# runner Er: the catalogue's class barbecuer (): def bakechickenwing (Self): printinfo ('Copy chicken wings! ') Def bakemutton (Self): printinfo ('Copy a mutton string! ') # Invoker: Waiter class waiter (): commandlist = [] def setorder (self, command): printinfo (' % s add order: % s' % (time. strftime ('% Y-% m-% d % H: % m: % s', time. localtime (time. time (), command. tostring () self. commandlist. append (command) def cancelorder (self, command): printinfo ('% s cancel order: % s' % (time. strftime ('% Y-% m-% d % H: % m: % s', time. localtime (time. time (), command. tostring () self. commandlist. remove (command) def every Y (Self): printinfo ('\ n notification:') for command in self. commandlist: Command. execute () def run (Self): printinfo ('run mobile phone address records') def clientui (): Boy = barbecuer () bakemuttoncommand1 = bakemuttoncommand (boy) bakemuttoncommand2 = bakemuttoncommand (boy) bakechickenwingcommand1 = bakechickenwingcommand (boy) GIRL = waiter () Girl. setorder (bakemuttoncommand1) Girl. setorder (bakemuttoncommand2) Girl. setorder (bakechickenwingcommand1) Girl. cancelorder (bakemuttoncommand1) Girl. required y () returnif _ name _ = '_ main _': clientui ();

Class Diagram:

Related Article

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.