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 Method
Imagine 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?
Solution Using Command Method
Let’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.
Action performed in receiver.
Following is the class diagram for the Command method
- Open/Closed Principle: We can introduce the new commmands 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 Commmand method.
- Encapsulation: It helps in encapsulating all the information needed to perform an action or an event.
- Complexity Increases: The complexity of the code increases as we are introducing certain layers between the senders and the recievers.
- Quantity of classes increases: For the 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.
- 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
- Factory Method - Python Design Patterns
- Observer method - Python Design Patterns
- Composite Method - Python Design Patterns
- Decorator Method - Python Design Patterns
- Abstract Factory Method - Python Design Patterns
- Builder Method - Python Design Patterns
- Singleton Method - Python Design Patterns
- Adapter Method - Python Design Patterns
- Prototype Method - Python Design Patterns
- Bridge Method - Python Design Patterns
- Facade Method - Python Design Patterns
- Proxy Method - Python Design Patterns
- Flyweight Method - Python Design Patterns
- Iterator Method - Python Design Patterns
- Memento Method - Python Design Patterns
- Visitor Method - Python Design Patterns
- Strategy Method - Python Design Patterns
- Template Method - Python Design Patterns
- State Method - Python Design Patterns
- Chain of Responsibility - Python Design Patterns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.