One among our largest challenges as software program builders is organizing our code in order that it’s simpler to increase and keep. The Command sample helps us try this by encapsulating all the info required to carry out an motion right into a single Command
object.
You would possibly acknowledge the Command sample as a result of we use it on a regular basis in our on a regular basis lives. instance is utilizing a distant management gadget to activate a tv, change channels, flip up the amount, and so forth. Each one in all these actions is encapsulated within the distant management gadget.
One thing else to notice about all of those actions is that they’re reversible: you’ll be able to activate the TV, and you may also flip it off. Moreover, a number of the actions should be executed so as: you will need to activate the TV earlier than you’ll be able to flip up the amount.
On this Java code problem, you will be taught concerning the Command design sample and see a number of examples of the sample in apply. I may even talk about how the Command sample implements two core ideas of the SOLID mannequin. The 2 ideas are the single-responsibility precept, which states {that a} class ought to have just one job, and the open-closed precept, which states that objects or entities must be open for extension however closed for modification.
What’s the Command sample?
The Command sample is without doubt one of the 23 design patterns launched with the Gang of Four design patterns. Command is a behavioral design sample, that means that it goals to execute an motion in a selected code sample.
When it was first launched, the Command sample was generally defined as callbacks for Java. Whereas it began out as an object-oriented design sample, Java 8 launched lambda expressions, permitting for an object-functional implementation of the Command sample. This text consists of an instance utilizing a lambda expression within the Command sample.
As with all design patterns, it is essential to know when to use the Command sample, and when one other sample is perhaps higher. Utilizing the unsuitable design sample for a use case could make your code extra sophisticated, not much less.
The Command sample within the JDK
We will discover many examples of the Command sample within the Java Growth Package, and within the Java ecosystem. One standard instance is utilizing the Runnable
purposeful interface with the Thread
class. One other is dealing with occasions with an ActionListener
. Let’s discover each of those examples.
The Command sample with Thread and Runnable
Runnable
is an interface that features the run()
technique. The next code snippet reveals the run()
technique’s signature. As you’ll be able to see, it’s potential to move a command within the run()
technique:
@FunctionalInterface
public interface Runnable
public summary void run();
Thread
is the most-used class that receives a Runnable
. Let’s have a look at how we are able to move a command to the Thread
class:
Runnable command = () -> System.out.println("Executing command!");
Thread thread = new Thread(command); // Setting command
thread.begin();
On this code, we implement the command habits within the run()
technique with a lambda expression. As an alternative of the lambda, we might use an nameless interior class, which is an unnamed class that implements Runnable
and the run()
technique. However that method would make the code extra verbose. Utilizing the lambda is extra concise and simpler to learn.
We then move the command to the Thread
class. Lastly, we execute the command by invoking the begin()
technique.
Here is the output we are able to anticipate from this code:
Executing command!
The Command sample with ActionListener
One other good instance within the JDK is the ActionListener
interface. I do know it is an older interface, however it’s appropriate for instance.
Within the following code, we create a JFrame
and a JButton
. We then set the motion within the button by invoking the addActionListener()
technique. On this case, we’ll simply change the textual content from “Click on me” to “Clicked.” Then, we’ll add the button to the body, and present the body with the button:
JFrame body = new JFrame();
JButton button = new JButton("Click on Me");
button.addActionListener(e -> button.setText("Clicked!")); // Command implementation
body.add(button);
body.pack();
body.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
body.setVisible(true);
Determine 1 reveals the outcomes of this code after the button is clicked.
Determine 1. ActionListener in motion.
Drive my bike! The Command sample in a Automobile interface
Now that you have seen examples of the Command sample within the JDK, let’s create our personal. First, check out the category diagram in Determine 2.
Determine 2. A diagram of the Command sample for a Automobile interface.
There are three components to the diagram, which I am going to clarify.
Command
The inspiration class for the Command sample is the Command
interface. We use this interface anytime we need to execute or revert a command:
public interface Command
void execute();
void revert();
Receiver
Subsequent, we have to create the category that has the habits to execute the command. We begin with the Automobile
interface, then create the Bike
and Truck
lessons to implement it:
public interface Automobile
void begin();
void cease();
void speed up();
public class Bike implements Automobile
@Override
public void begin()
System.out.println("Beginning bike...");
@Override
public void cease()
System.out.println("Stopping bike...");
@Override
public void speed up()
System.out.println("Accelerating bike...");
public class Truck implements Automobile
@Override
public void begin()
System.out.println("Beginning truck...");
@Override
public void cease()
System.out.println("Stopping truck...");
@Override
public void speed up()
System.out.println("Accelerating truck...");
@Override
public void decelerate()
System.out.println("Decelerating truck...");
Additionally discover that the Automobile
interface makes the code extra versatile and simpler to alter: we might simply add one other car comparable to Automotive
that implements the Automobile
interface. This a part of the Command sample is a superb instance of the open-closed SOLID precept. (Keep in mind that this precept states that objects or entities must be extensible.)
Invoker
Now, we’ve the Bike
and Truck
habits however we’d like a category to execute it. In our case, this class would be the GhostRider
. GhostRider
will drive the Bike
and Truck
lessons.
GhostRider
receives the command within the constructor and invokes the execute()
technique from the command into the takeAction()
and revertAction()
strategies:
public class GhostRider
Command command;
public GhostRider(Command command)
this.command = command;
public void setCommand(Command command)
this.command = command;
public void takeAction()
command.execute();
public void revertAction()
command.revert();
Implementing instructions within the Command sample
Now, let’s create the StartMotorcycle
, AccelerateMotorcycle
, and StartAllVehicles
instructions. Every command implements the Command
interface and receives Automobile
within the constructor. Then, it invokes the tactic that corresponds to every command class from Automobile
into the execute()
technique:
public class StartMotorcycle implements Command
Automobile car;
public StartMotorcycle(Automobile car)
this.car = car;
public void execute()
car.begin();
@Override
public void revert()
car.cease();
public class AccelerateMotorcycle implements Command
Automobile car;
public AccelerateMotorcycle(Automobile car)
this.car = car;
public void execute()
car.speed up();
@Override
public void revert()
car.decelerate();
import java.util.Record;
public class StartAllVehicles implements Command
Record<Automobile> autos;
public StartAllVehicles(Record<Automobile> autos)
this.autos = autos;
public void execute()
autos.forEach(car -> car.begin());
@Override
public void revert()
autos.forEach(car -> car.cease());
Run the instructions
It is time to run our instructions! For this, we first instantiate the Bike
class that has the Command
habits, then move it into every Command
implementation.
Discover that we’re additionally utilizing the StartAllVehicles
command to start out (and cease) a number of autos directly.
Then, we instantiate the GhostRider
class that may execute every command. Lastly, we invoke the takeAction()
and revertAction()
strategies:
public class RideVehicle
public static void foremost(String[] args)
Automobile bike = new Bike();
StartMotorcycle startCommand = new StartMotorcycle(bike);
GhostRider ghostRider = new GhostRider(startCommand);
ghostRider.takeAction();
AccelerateMotorcycle accelerateCommand = new AccelerateMotorcycle(bike);
ghostRider.setCommand(accelerateCommand);
ghostRider.takeAction();
ghostRider.revertAction();
Automobile truck = new Truck();
Record<Automobile> autos = Record.of(bike, truck);
StartAllVehicles startAllVehicles = new StartAllVehicles(autos);
startAllVehicles.execute();
startAllVehicles.revert();
Right here is the output from this code:
Beginning bike...
Accelerating bike...
Decelerating bike...
Beginning bike...
Beginning truck...
Stopping bike...
Stopping truck…
When to make use of the Command sample
A vital rule for design patterns is to know when to make use of them. Irrespective of how nice a sample is, implementing it for the unsuitable use case will make your code a lot worse. Listed here are some pointers for utilizing the Command sample:
- You could have a number of instructions that must be applied individually based mostly on the SOLID ideas of single-responsibility and open-closed design.
- It’s essential create reversible instructions, comparable to including and eradicating an merchandise from a procuring cart.
- You want to have the ability to create logs every time a command is executed. Every command within the Command sample is encapsulated, so creating logs is simple.
- You want to have the ability to execute a number of instructions directly. You possibly can simply add a
Queue
,Record
, orSet
right into a command’s implementation and execute them.
What to recollect concerning the Command sample
To summarize, keep in mind the next concerning the Command sample:
- It applies the SOLID ideas of single-responsibility and open-closed design.
- It encapsulates and decouples the habits of instructions, which makes your code extra extensible.
- It is used within the JDK with the
Thread
class andRunnable
andActionListener
interfaces. - It encapsulates the habits of instructions inside a single
Command
implementation. - It enables you to execute and revert single instructions.
- It enables you to execute and revert a number of instructions collectively.
Copyright © 2022 IDG Communications, Inc.