Python design mode 24th Day "command mode"

Source: Internet
Author: User

1. Usage Scenarios

(1) The calling process is cumbersome and requires encapsulation

(2) Call parameters need to be processed encapsulation

(3) need to add additional functions, such as log, cache, operation record, etc.

2. Code implementation

#!/usr/bin/env python#! _*_ coding:utf-8 _*_ fromAbcImportAbcmeta, AbstractmethodclassReceiver (object):" "at this point the basic class" "    def __init__(self):Pass    defAction_one (self):Print "Action One ..."    defAction_two (self):Print "Action ..."classCommand (object):" "At this point the command abstract class" "    def __init__(self, receiver): Self.receiver=receiver @abstractmethoddefExecute (self):PassclassOnecommand (Command):defExecute (self): Self.receiver.action_one ()classTwocommand (Command):defExecute (self): Self.receiver.action_two ()classInvoker (object):" "This is the final call to the target" "    def __init__(self):Pass    defCreateCommand (Self, Command): Self.command=CommandreturnSelf.commandif __name__=="__main__": Receiver=Receiver () Onecommand=Onecommand (receiver) Twocommand=Twocommand (receiver) Invoker=Invoker () Invoker.createcommand (Onecommand). Execute () Invoker.createcommand (Twocommand). Execute ()

Results:

/users/liudaoqiang/pycharmprojects/numpy/venv/bin/python/users/liudaoqiang/project/python_project/day24_ command/command_test.pyaction one...action ... Process finished with exit code 0

Python design mode 24th Day "command mode"

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.