Python Design Patterns — Command Pattern
Command pattern is a behavioral design pattern in which the object is encapsulate all the information to be executed at later time.
To understand this pattern let’s take an example of signing up for an account.
So let’s assume that registration is three step procedure. First step is to fill basic details, second step to fill business details and then final step to fill payment details. Initially object of Command is created and will be used to collect all information. Once all information of these three steps are filled you would hit Register button and Command object which has encapsulated all the information in it and execute method will be executed to register the account.
- Command - Executes receiver’s action on invoking by Invoker
- Client: Create the Invoker object and also sets the receiver object
- Invoker: This actually manage the commands by specific invoke method.
- Receiver: Ultimate action performer.
Understanding of command pattern —
- Object of Command has knowledge of Receiver so that can call method of Receiver object.
- All the values will be stored in object of Command class and those values will be passed to Receiver object method.
- Invoker has details to execute command.
- Client object passes a Receiver object while creating Command object.
How to implement —
In above example,
NotificationService (Receiver) is ultimate executer of the action.
BaseCommand is the command interface. We have two concrete command class — SMSCommand and EMailCommand
NotificationInvoker is invoker class which actually call the execute method of the command object. Here all the commands are stored in notification_history object. Which cab be used to re-execute all the commands as those command object contains all the information to execute again.
Here Client class is not implemented as we are the client using all above classes as all steps are mentioned in end of the code.
First we create objects of Receiver and Invoker class. We also create objects of EMailCommand or SMSCommand class with passing arguments and pass it to invoke method of Invoker class object. When you invoke any command Invoker will call execute method of command object and command will call the method of Receiver object with all the information are stored in Command object.
In above example, We are executing methods on instant basis. But we can modify Invoker where we can set scheduling information that way we can make it asynchronous. We can also modify Command classes where we can provide facility to consolidate all the information in different way.
This pattern can be used in our projects with few tweaks according to your needs.
Benefits of this pattern —
- Re-perform or Rollback operation can be performed.
- Queue system can be built to execute commands in sequence.
- New type of command can be introduced with very few changes.
Originally published on aaravtech.com