JAVA Developer Guide to begin with Command Design Pattern
Have you ever wondered how a music conductor can orchestrate a group of musicians without even knowing how all those musical instruments work? Or how you can drive a car without even knowing the internals working like how the engine works, how it needs fuel to enter the engine, how the valves work, etc.
Similarly, in most programming scenarios, you want to hide the implementation so that when you look at the topmost problem, it only contains a manageable portion of instructions or code. Command pattern helps in separating the execution of commands from commands themselves, hiding the actual implementation of how the command is executed.
What is Command Pattern?
The command pattern is one of the behavioral design patterns from well-known GoF design patterns. In this design pattern, an object (command) is used to encapsulate all the information (eg. method name and parameters) requires to trigger an event or perform any action on another object (receiver) at a later time and this command object is provided to another object (invoker) to perform the action based on client requirements.
It separates the invoker object that invokes the operation from the receiver object that operates, and thus eliminates hard wiring between them. It makes it easy to add new commands because existing classes remain unchanged.
A particular method call can be transformed into a standalone object using the Command pattern. This adaptation allows us to pass commands as method arguments, store them inside other objects, exchange linked commands at runtime, and many more intriguing uses.
A command can be serialized, which refers to the process of transforming it into the bytes that can be readily written to a file or database, just like any other streamed object.
These bytes may later be retrieved to serve as the initial command object. As a result, you can schedule and postpone command execution. There’s still more, though! You can queue, log, or send commands across the network in the same way.
Command Pattern JAVA Example
In this example, the Command pattern helps to control the movement of the video game player using a joystick. Based on the inputs being received for joysticks, we can invoke the commands which will move the player.
Receiver Definition
Created an interface for the Player to list the instructions available.
VideoGamePlayer class is implementing the Player interface and providing concrete methods for all movement instructions.
Command Definition
Action interface is having a method doAction which is going to be implemented by all concrete commands.
Concrete Command (Implementations)
Here in each type of concrete command, we are passing Player in the constructor argument and implementing the doAction method to perform a specific action.
Invoker Definition
Created Controller interface listing all the invoker-specific methods.
Concrete Invoker (Implementation)
Provided all the commands to JoyStickController class in the constructor arguments and implemented all the methods to execute the respective command. Here this concrete invoker does not need to check the instructions for the receiver object thus eliminating all the hard wiring.
Client Implementation in Action
In Client class main method, created Player object (receiver) with VideoGamePlayer implementation. Created all the actions (commands) required by JoyStickController (concrete invoker). Based on the input received from the client standard input, the invoker is indirectly sending instructions to the receiver via commands.
Source Code Available at: https://github.com/s3c-d43m0n/Desing-Patterns-in-JAVA/tree/main/Behavioral/Command
Key Points in the Implementation
- Clients need to create all the objects in a specific order: first, create receivers, then create commands, and last create invokers.
- Declare the command interface with a single execution method.
- Create separate commands for each action defined in the receiver.
- Provide all the commands to the invoker object and link with the appropriate action.
Note: This pattern is also known as the Action or Transaction pattern.
What’s your favorite design pattern or if you’d like to understand better any other pattern or topic, feel free to reach out to me on LinkedIn or Google Form and I’ll try to cover it next time!
Please share this with all your Medium friends and hit that👏 button below to spread it around even more. Please follow me for future updates. Thanks for reading.