How to implement Command pattern in Java?

Photo by Pressmaster on

Let’s say you run a company.

You want to give commands to your manager to instruct developers to write code for an application and testers to test it.

Also you want to tell the manager what to do dynamically.

You give him only the command but the manager neither knows who is going to execute the command nor what the command is until the last moment (at run time).

You decide the type of command and who is going to execute it.

Command pattern perfectly suits this scenario.

Command patterns lets you encapsulate a request into an object .It decides who is going to execute the request and what is the request – dynamically

Here is the client code :

package behavioural.command.pattern;

public class Client {

	public static void main(String a[]) {
		DevelopApplicationCommand developCommand = new DevelopApplicationCommand(new Developer("Agathya"));
		TestApplicationCommand testCommand = new TestApplicationCommand(new Tester("Vimal"));
		Manager manager = new Manager();

There are two commands here :

DevelopApplicationCommand to instruct developers to write code.

TestApplicationCommand to instruct testers to test code.

A manager is going to execute these commands.

But the manager neither knows who is going to execute the request and what the request is until run time.

All the manager does is call the method giveCommandToTeam() with a command object as parameter.

This command object knows the receiver which is also configured at client code dynamically.

Eg) DevelopApplicationCommand knows that the receiver is a Developer who is passed as a constructor parameter to the command.

To reiterate , we decouple both the receiver of the command and execution of the command from the invoker (Manager).

Here is the command interface. It has one method execute():

package behavioural.command.pattern;

public interface Command {
	void execute();


Here is the DevelopApplicationCommand class:

package behavioural.command.pattern;

public class DevelopApplicationCommand implements Command {

	private Developer developer;

	public DevelopApplicationCommand(Developer developer) {

		this.developer = developer;

	public void execute() {



The receiver of this command is passed through the constructor ( a developer) and the command is also executed by the receiver (this.developer.developCode()).

Here is the TestApplicationCommand:

package behavioural.command.pattern;

public class TestApplicationCommand implements Command {

	private Tester tester;

	public TestApplicationCommand(Tester tester) {

		this.tester = tester;

	public void execute() {




The receiver of the above command is a tester which can be configured at run time. The same receiver executes the command when the invoker invokes the command.

Here is the Invoker(Manager) class:

package behavioural.command.pattern;

public class Manager {

	public void giveCommandToTeam(Command command) {



As you see all the manager does is invoke the command. He neither knows about the receiver or what the receiver is going to do . Those are decoupled .

Here are the receivers .

The developer:

package behavioural.command.pattern;

public class Developer {

	private String name;

	public Developer(String name) { = name;


	public void developCode() {

		System.out.println("Developer " + name + " writing code");


Here is the tester:

package behavioural.command.pattern;

public class Tester {

	private String name;

	public Tester(String name) { = name;


	public void testApplication() {

		System.out.println("Tester " + name + " testing application");


That’s it.

You have made your manager execute commands and he knows nothing about the command until you say it(in the client code). Neither he knows about who is going to carry out the command . Only you say it.

According to the Gang of Four ,

The manager is the Invoker ,

The DevelopApplicationCommand and TestApplicationCode are ConcreteCommands,

The developer and the tester are the Receivers.

Command and Client classes are the same Command and Client classes as suggested by them.

So a Client creates a ConcreteCommand (which is passed a Receiver ) and then creates an Invoker.

The Invoker then executes the Command indirectly through the Receiver.

Further to this you can store the history of your commands , or store your commands in a queue and execute them one by one or undo your commands . Those are not dealt with in this post.

Quoting the Gang of Four :

Encapsulate the request as an object , thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Here is the code:



, ,



Leave a Reply

%d bloggers like this: