Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Command pattern in Python Design pattern

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to realize the command pattern in the Python design pattern, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to realize the command pattern in the Python design pattern. Let's take a look.

1. Command mode

The purpose of the command mode is to decouple the object that invokes the operation (the caller) and the object that provides the implementation (receiver).

The idea of command mode is to insert a command class (Command) between the caller and the receiver, which defines an execute interface, and the interface actually invokes specific methods in the receiver to execute specific commands, so that multiple different receivers can be extended by extending command subclasses.

This decouples the caller who invokes this command from the recipient of the command.

Advantages:

Good encapsulation, each command is encapsulated, for the client, what function is needed to invoke the corresponding command, without knowing exactly how the command is executed.

The expansibility is good, the operation is generally encapsulated in the receiver class, and the command class encapsulates these basic operations twice.

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, but also a large number of command classes to call.

two。 Application scenario

Menu-driven text editor: the caller is the menu and the recipient is the edited document.

3. Code example

Entity roles:

Abstract command base class (Command): an abstract class of commands that defines the interface of commands and can be understood as a base class.

Specific command implementation class (ConcreteCommand): the command interface implements the object, which usually holds the receiver and invokes the receiver's function to complete the operation to be performed by the command.

Command recipient (Receiver): the object that actually receives and executes specific commands. Any class can become a recipient, as long as it implements the corresponding functionality required by the command.

Command caller (Invoker): invokes the command and sends the command to the recipient. It usually holds command objects, and can hold a lot of command objects, which is equivalent to the entry of command objects.

Command assembler (Client): the client creates specific command objects, assembles command objects and recipients.

Class Command: "declare command mode interface" def _ _ init__ (self " Obj): self.obj = obj def execute (self): passclass ConcreteCommand (Command): "implement command mode interface"def execute (self): self.obj.run () class Invoker:" Interface to accept and execute 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 1')) test.add_command (cmd1) cmd2 = ConcreteCommand (Receiver ('Command 2') test.add_command (cmd2) cmd3 = ConcreteCommand (Receiver ('Command 3')) test .add _ command (cmd3) test.run_command () if _ _ name__ = ='_ _ main__': client () this is the end of the article on "how to implement Command patterns in Python Design patterns" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to realize the command pattern in Python design pattern". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report