Project Technical Lead
Java design pattern: Command
Today we will look at another Java design pattern from the category of behavioral patterns – Command. Design patterns in this category deal with the communication (interaction) between objects and their responsibility.
What is the Command design pattern?
Command design pattern is a pattern that wraps requirements or operations, along with the information to perform them, into separate objects. This information most often includes the method name of the corresponding object along with the method parameters. There are always concepts associated with a pattern Client, Command, Receiver, Invoker. The client decides what order to execute and when. In order to execute the command, it moves the command object of the invoker object. The invoker knows nothing about a particular object, it only knows its interface. Its task is to execute the command by calling the execute method and, if necessary, to store the details of the invoked operation. The recipient then executes the job from the command object.
What problem does the Command design pattern solve?
It solves the problem of separating the object that invokes an operation (the invoker) from the object that performs that operation (the receiver), the main goal being to create a flexible connection between the objects that trigger requests and those that process them. These objects contain all the information needed to perform a given operation in the background, and in this way it is possible to work with client requests with different parameters as needed. Requests can be queued for processing, logged, or even reverse operations can be implemented.
Example of Command implementation in Java
The design pattern is a bit more complicated to understand, so I have prepared an example where we will explain the Client, Command, Receiver, Invoker roles in more detail. We will write a simple program that will control the flashlight through commands.
Prikaz.java
package designpatterns;
// Command rozhranie
public interface Prikaz {
void vykonat();
}
The Prikaz interface defines the vykonat() operation that all concrete commands will implement.
ZapnutSvetlo.java
package designpatterns;
// Konkrétny príkaz
public class ZapnutSvetlo implements Prikaz {
private Baterka baterka;
public ZapnutSvetlo(Baterka baterka) {
this.baterka = baterka;
}
@Override
public void vykonat() {
baterka.zapnut();
}
}
VypnutSvetlo.java
package designpatterns;
// Konkrétny príkaz
public class VypnutSvetlo implements Prikaz {
private Baterka baterka;
public VypnutSvetlo(Baterka baterka) {
this.baterka = baterka;
}
@Override
public void vykonat() {
baterka.vypnut();
}
}
Specifically, the ZapnutSvetlo and VypnutSvetlo commands implement the Prikaz interface and contain a reference to a specific Baterka object that will perform the corresponding operation.
Baterka.java
package designpatterns;
// Receiver
public class Baterka {
public void zapnut() {
System.out.println("Baterka svieti");
}
public void vypnut() {
System.out.println("Baterka je vypnuta");
}
}
The Baterka class represents a Receiver and contains the methods that will be called when the commands are executed.
Vypinac.java
package designpatterns;
// Invoker
public class Vypinac {
private Prikaz prikaz;
public void nastavPrikaz(Prikaz prikaz) {
this.prikaz = prikaz;
}
public void stlacitTlacidlo() {
prikaz.vykonat();
}
}
The class Vypinac represents an Invoker and contains a reference to an object of type Prikaz and has a method stlacitTlacidlo() that calls the vykonat() operation of the corresponding command.
Main.java
import designpatterns.*;
// Klient
public class Main {
public static void main(String[] args) {
// Receiver
Baterka baterka = new Baterka();
// Command(s)
Prikaz zapnutSvetlo = new ZapnutSvetlo(baterka);
Prikaz vypnutSvetlo = new VypnutSvetlo(baterka);
// Invoker
Vypinac vypinac = new Vypinac();
// Nastavenie a spustenie príkazu.
vypinac.nastavPrikaz(zapnutSvetlo);
vypinac.stlacitTlacidlo();
// Nastavenie a spustenie iného príkazu.
vypinac.nastavPrikaz(vypnutSvetlo);
vypinac.stlacitTlacidlo();
}
}
The Main class represents the client. In the main method, we create commands to control the flashlight and link them to the receiver that will process them. We will also create an invoker object – a switch, through which the client sets a specific command and it will be executed after calling the stacitTlacidlo() method. Simply put, the client sends commands to the switch that controls the flashlight.
The output of this example is:
Summary
The Command design pattern is quite often used when we need to get rid of the dependency of a request with a direct concrete implementation. It has a wide range of applications in GUI, network communication, database transactions, etc.
We have prepared the files with the above example in the form of code that you can run directly in Java. Download the Java Command code here.
If you’re a Java developer looking for work, check out our employee benefits and respond to our job offers.