KEMBAR78
Implementation of Command Design Pattern | PDF | Method (Computer Programming) | Interface (Computing)
0% found this document useful (0 votes)
50 views6 pages

Implementation of Command Design Pattern

The Command pattern encapsulates actions or requests as objects. This allows clients to parameterize other objects with different actions, queue or log requests, and support undoable operations. A command defines an interface for executing an operation, and concrete command classes implement the operation using a receiver object. A client creates a command object and passes it to an invoker object, which executes the command. This decouples the object requesting the operation from the objects that know how to perform it.

Uploaded by

jabi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views6 pages

Implementation of Command Design Pattern

The Command pattern encapsulates actions or requests as objects. This allows clients to parameterize other objects with different actions, queue or log requests, and support undoable operations. A command defines an interface for executing an operation, and concrete command classes implement the operation using a receiver object. A client creates a command object and passes it to an invoker object, which executes the command. This decouples the object requesting the operation from the objects that know how to perform it.

Uploaded by

jabi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Command Pattern

The Command Design Pattern wraps a request in an object as command and pass it
to a command invoker object. When invoker receives a request, it looks for the
appropriate object which is capable of handling this request and passes the command
to the corresponding object for execution. This allows clients to be parameterized with
different requests.

In command pattern, a client just gives instructions to invoke without knowing how it is
going to be executed. It supports decoupling between a client object which request a
task and the receiver object which actually performs the task. This pattern is not
focused on the sequence of the tasks stored, but on hiding the details/implementation of
the actions performed.

Implementation of Command Design Pattern


1. Define a Command interface having an execute method execute().
2. All command objects must implements a Command interface. The execute
method delegates the request to a receiver to execute the command.
3. A receiver class holds the logic of performing any specific task requested
as command. It is called from execute method of command object.
4. The client creates a set of command objects and associates receiver with
it. Client passes commands to invoker to store it. Later, client calls invoker to
execute the commands.

1
Let's use a remote control as the example. Our remote is the center of home automation
and can control everything. We'll just use a light as an example, that we can switch on
or off, but we could add many more commands.

1. First we'll create our command interface.

//Command
public interface Command{
public void execute();
}

2. Now let's create two concrete commands. One will turn on the lights, another
turns off lights.

//Concrete Command
public class LightOnCommand implements Command{
//reference to the light
Light light;
public LightOnCommand(Light light){
this.light = light;
}
public void execute(){
light.switchOn();
}
}

//Concrete Command
public class LightOffCommand implements Command{
//reference to the light
Light light;
public LightOffCommand(Light light){
this.light = light;
}
public void execute(){
light.switchOff();
}
}

2
3. Light is our receiver class, so let's set that up now.

//Receiver
public class Light{
private boolean on;
public void switchOn(){
on = true;
}
public void switchOff(){
on = false;
}
}

4. Our invoker in this case is the remote control.


//Invoker
public class RemoteControl{
private Command command;
public void setCommand(Command command){
this.command = command;
}
public void pressButton(){
command.execute();
}
}

5. Finally we'll set up a client to use the invoker

//Client
public class Client{
public static void main(String[] args) {
RemoteControl control = new RemoteControl();
Light light = new Light();
Command lightsOn = new LightsOnCommand(light);
Command lightsOff = new LightsOffCommand(light);
//switch on
control.setCommand(lightsOn);
control.pressButton();
//switch off
control.setCommand(lightsOff);

3
control.pressButton();
}
}
Watch Out for the Downsides
This pattern ends up forcing a lot of Command classes that will make your design look
cluttered - more operations being made possible leads to more command classes.
Intelligence required of which Command to use and when leads to possible
maintainence issues for the central controller.

Another example:

Command.java
1 public interface Command {
2 public void execute();
3 }

DrawCircle and DrawRectangle are concrete implementation of Command interface.


DrawCircle .java
1
public class DrawCircle implements Command {
2 ShapeDrafter drafter;
3
4 public DrawCircle(ShapeDrafter drafter){
5 this.drafter = drafter;
6 }
7
8 public void execute(){
drafter.drawCircle();
9 }
10 }
11

DrawRectangle.java
public class DrawRectangle implements Command {
1 ShapeDrafter drafter;
2
3 public DrawRectangle(ShapeDrafter drafter){
4 this.drafter = drafter;
5 }
6
7 public void execute(){
drafter.drawRectangle();
8 }
9 }

4
10
11

ShapeDrafter is the receiver class which draws circle and rectangle.


ShapeDrafter.java
1
public class ShapeDrafter {
2 public void drawRectangle(){
3 System.out.println("Drawing a Rectangle on Screen");
4 }
5
6 public void drawCircle(){
7 System.out.println("Drawing a Circle on Screen");
}
8 }
9

CommandInvoker.java
1
2 import java.util.List;
3 import java.util.ArrayList;
4
public class CommandInvoker {
5 private List<Command> commandList = new ArrayList<Command>();
6
7 public void addCommand(Command c){
8 commandList.add(c);
9 }
10
11 public void executeCommands(){
for(Command c : commandList){
12 c.execute();
13 }
14 }
15 }
16

CommandPatternExample class creates command objects and pass it to


CommandInvoker for execution.
public class CommandPatternExample {
1 public static void main(String args[]){
2 ShapeDrafter drafter = new ShapeDrafter();
3
4 Command rectangleCommand = new DrawRectangle(drafter);
5 Command circleCommand = new DrawCircle(drafter);
6
7 CommandInvoker invoker = new CommandInvoker();
invoker.addCommand(circleCommand);
8 invoker.addCommand(rectangleCommand);

5
9
10 invoker.executeCommands();
11 }
}
12
13
14
Output

Drawing a Circle on Screen


Drawing a Rectangle on Screen

You might also like