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"