Command Method - Python Design Patterns Last Updated : 06 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Command Method is Behavioral Design Pattern that encapsulates a request as an object, thereby allowing for the parameterization of clients with different requests and the queuing or logging of requests. Parameterizing other objects with different requests in our analogy means that the button used to turn on the lights can later be used to turn on stereo or maybe open the garage door. It helps in promoting the "invocation of a method on an object" to full object status. Basically, it encapsulates all the information needed to perform an action or trigger an event. Problem without using Command MethodImagine you are working on a code editor. Your current task is to add the new buttons in the toolbar of the editor for various different operations. It's definitely easy to create a single Button Class that can be used for the buttons. As we know that all the buttons used in the editor look similar, so what should we do? Should we create a lot of sub-classes for each place where the button is used? Problem-without-Command-methodSolution Using Command MethodLet's have a look at the solution for the above-described problem. It's always a good idea to divide the software into different layers which helps in easy coding as well as debugging. The command pattern suggests that objects shouldn’t send these requests directly. Instead, you should extract all of the request details, such as the object being called, the name of the method and the list of arguments into a separate command class with a single method that triggers this request. Python3 """Use built-in abc to implement Abstract classes and methods""" from abc import ABC, abstractmethod """Class Dedicated to Command""" class Command(ABC): """constructor method""" def __init__(self, receiver): self.receiver = receiver """process method""" def process(self): pass """Class dedicated to Command Implementation""" class CommandImplementation(Command): """constructor method""" def __init__(self, receiver): self.receiver = receiver """process method""" def process(self): self.receiver.perform_action() """Class dedicated to Receiver""" class Receiver: """perform-action method""" def perform_action(self): print('Action performed in receiver.') """Class dedicated to Invoker""" class Invoker: """command method""" def command(self, cmd): self.cmd = cmd """execute method""" def execute(self): self.cmd.process() """main method""" if __name__ == "__main__": """create Receiver object""" receiver = Receiver() cmd = CommandImplementation(receiver) invoker = Invoker() invoker.command(cmd) invoker.execute() OutputAction performed in receiver.Class DiagramFollowing is the class diagram for the Command method Class-diagram-Command-MethodAdvantages Open/Closed Principle: We can introduce the new commands into the application without breaking the existing client's code.Single Responsibility Principle: It's really easy to decouple the classes here that invoke operations from other classes.Implementable UNDO/REDO: It's possible to implement the functionalities of UNDO/REDO with the help of Command method.Encapsulation: It helps in encapsulating all the information needed to perform an action or an event.DisadvantagesComplexity Increases: The complexity of the code increases as we are introducing certain layers between the senders and the receivers.Quantity of classes increases: For each individual command, the quantity of the classes increases.Concrete Command: Every individual command is a ConcreteCommand class that increases the volume of the classes for implementation and maintenance.Applicability Implementing Reversible operations: As the Command method provides the functionalities for UNDO/REDO operations, we can possibly reverse the operations.Parameterization: It's always preferred to use Command method when we have to parameterize the objects with the operations.Further Read - Command Method in Java Comment More infoAdvertise with us Next Article Command Method - Python Design Patterns chaudhary_19 Follow Improve Article Tags : Python Design Pattern System Design python-design-pattern Practice Tags : python Similar Reads Python Design Patterns Tutorial Design patterns in Python are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best pra 7 min read Creational Software Design Patterns in PythonFactory Method - Python Design Patterns Factory Method is a Creational Design Pattern that allows an interface or a class to create an object, but lets subclasses decide which class or object to instantiate. Using the Factory method, we have the best ways to create an object. Here, objects are created without exposing the logic to the cli 4 min read Abstract Factory Method - Python Design Patterns Abstract Factory Method is a Creational Design pattern that allows you to produce the families of related objects without specifying their concrete classes. Using the abstract factory method, we have the easiest ways to produce a similar type of many objects. It provides a way to encapsulate a group 4 min read Builder Method - Python Design Patterns Builder Method is a Creation Design Pattern which aims to "Separate the construction of a complex object from its representation so that the same construction process can create different representations." It allows you to construct complex objects step by step. Here using the same construction code 5 min read Prototype Method Design Pattern in Python The Prototype Method Design Pattern in Python enables the creation of new objects by cloning existing ones, promoting efficient object creation and reducing overhead. This pattern is particularly useful when the cost of creating a new object is high and when an object's initial state or configuratio 6 min read Singleton Method - Python Design Patterns Prerequisite: Singleton Design pattern | IntroductionWhat is Singleton Method in PythonSingleton Method is a type of Creational Design pattern and is one of the simplest design patterns available to us. It is a way to provide one and only one object of a particular type. It involves only one class t 5 min read Structural Software Design Patterns in PythonAdapter Method - Python Design Patterns Adapter method is a Structural Design Pattern which helps us in making the incompatible objects adaptable to each other. The Adapter method is one of the easiest methods to understand because we have a lot of real-life examples that show the analogy with it. The main purpose of this method is to cre 4 min read Bridge Method - Python Design Patterns The bridge method is a Structural Design Pattern that allows us to separate the Implementation Specific Abstractions and Implementation Independent Abstractions from each other and can be developed considering as single entities.The bridge Method is always considered as one of the best methods to or 5 min read Like