程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python design pattern - behavioral pattern - command pattern

編輯:Python

Catalog

List of articles

  • Catalog
  • Command mode
  • Application scenarios
  • Code example

Command mode

The purpose of the command pattern is to decouple the object that invokes the operation ( caller ) And objects that provide implementation ( The receiver ).

The idea of command mode is to insert a command class between the caller and the receiver (Command), This command class defines a execute Interface , And the interface actually calls specific methods in the receiver to execute specific commands , In this way, multiple different receivers can be extended by extending the command subclass .

In this way, the caller who invokes this command is decoupled from the command receiver .

advantage :

  • Good encapsulation , Each command is encapsulated , For the client , Call the corresponding command when you need any function , Without knowing how the command is executed .
  • Good scalability , In the receiver class, the operation is generally encapsulated in the most basic way , The command class encapsulates these basic operations .
  • Good reusability , When adding new commands , The writing of command classes generally does not start from scratch , There are a large number of receiver classes to call , There are also a large number of command classes to call .

Application scenarios

  • Menu driven text editor : The caller is the menu , The recipient is the edited document .

Code example


Entity role :

  • Abstract command base class (Command): Abstract class of command , Define the interface of the command , It can be understood as a base class .
  • Concrete command implementation class (ConcreteCommand): Command interface implementation object , Usually the receiver , And call the receiver's function to complete the operation to be executed .
  • Command the receiver (Receiver): The object that actually receives and executes specific commands . Any class can become a receiver , As long as it can realize the corresponding functions required by the command .
  • Order caller (Invoker): Call the command , Then send the command to the receiver . Usually holds the command object , Can hold many command objects , It is equivalent to using the entry of the command object .
  • Command the assembler (Client): The client will create a specific command object , Assemble command objects and receivers .
class Command:
""" Declare the command mode interface """
def __init__(self, obj):
self.obj = obj
def execute(self):
pass
class ConcreteCommand(Command):
""" Implement command mode interface """
def execute(self):
self.obj.run()
class Invoker:
""" Interface for receiving and executing commands """
def __init__(self):
self._commands = []
def add_command(self, cmd):
self._commands.append(cmd)
def remove_command(self, cmd):
self._commands.remove(cmd)
def run_command(self):
for cmd in self._commands:
cmd.execute()
class Receiver:
""" Specific actions """
def __init__(self, word):
self.word = word
def run(self):
print(self.word)
def client():
""" Assembler """
test = Invoker()
cmd1 = ConcreteCommand(Receiver(' Command one '))
test.add_command(cmd1)
cmd2 = ConcreteCommand(Receiver(' Command two '))
test.add_command(cmd2)
cmd3 = ConcreteCommand(Receiver(' Three orders '))
test.add_command(cmd3)
test.run_command()
if __name__ == '__main__':
client()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved