How to implement Command Design Pattern in Java using simple example ?.

Program to implement Command Design Pattern in Java using simple example.

1. Device.java

package com.hubberspot.designpatterns.command;
// Let us create a Device interface having
// methods which concrete device can perform
// the methods are implemented by concrete
// devices such as Television , Radio etc...
// Making device on, off, volume up, volume down etc
// will be implemented by concrete devices.
public interface Device {
public void on();
public void off();
public void volumeUp();
public void volumeDown();
public void channelUp();
public void channelDown();
}

package com.hubberspot.designpatterns.command;
// Each command on the remote control
// say turn tv on , turn tv off etc ...
// will have to implement Command interface
// having just an simple execute method.
public interface Command {
public void execute();
}

5. TurnOn.java

package com.hubberspot.designpatterns.command;
// TurnOn Command is the concrete implementation
// of Command interface also having a Device with it
// which is been set up by the constructor.
// its implemented execute method will call the
// turnOn Button on the remote control and will
// start the television or any other device.
public class TurnOn implements Command {
private Device device;
public TurnOn(Device device) {
this.device = device;
}
@Override
public void execute() {
device.on();
}
}

package com.hubberspot.designpatterns.command;
// It will act as a RemoteControl to the concrete
// devices such as Television and Radio.
public class RemoteControl {
private Command command;
public RemoteControl(Command command) {
this.command = command;
}
public void press() {
this.command.execute();
}
}

12. ChannelDown.java

package com.hubberspot.designpatterns.command;
public class TestThePattern {
public static void main(String[] args) {
System.out.println("\nPlaying with Television .... ");
// Create a Television object
Device television = new Television();
// pass the device information to
// TurnOn Command so that it invokes based on this television
TurnOn turnOnCommand = new TurnOn(television);
// pass the command information to remotecontrol
RemoteControl onButton = new RemoteControl(turnOnCommand);
// when we press the button based on turnOnCommand
// television is made on
onButton.press();
// Same is below for every other commands
TurnOff turnOffCommand = new TurnOff(television);
RemoteControl offButton = new RemoteControl(turnOffCommand);
offButton.press();
System.out.println();
VolumeUp volumeUpCommand = new VolumeUp(television);
RemoteControl volumeUpButton = new RemoteControl(volumeUpCommand);
volumeUpButton.press();
volumeUpButton.press();
volumeUpButton.press();
System.out.println();
VolumeDown volumeDownCommand = new VolumeDown(television);
RemoteControl volumeDownButton = new RemoteControl(volumeDownCommand);
volumeDownButton.press();
volumeDownButton.press();
System.out.println("\nPlaying with Radio .... ");
Device radio = new Radio();
turnOnCommand = new TurnOn(radio);
onButton = new RemoteControl(turnOnCommand);
onButton.press();
ChannelUp channelUpCommand = new ChannelUp(radio);
RemoteControl channelUpButton = new RemoteControl(channelUpCommand);
channelUpButton.press();
}
}

Output of the program :

Program to implement Command Design Pattern in Java using simple example.

1. Device.java

package com.hubberspot.designpatterns.command;
// Let us create a Device interface having
// methods which concrete device can perform
// the methods are implemented by concrete
// devices such as Television , Radio etc...
// Making device on, off, volume up, volume down etc
// will be implemented by concrete devices.
public interface Device {
public void on();
public void off();
public void volumeUp();
public void volumeDown();
public void channelUp();
public void channelDown();
}

package com.hubberspot.designpatterns.command;
// Each command on the remote control
// say turn tv on , turn tv off etc ...
// will have to implement Command interface
// having just an simple execute method.
public interface Command {
public void execute();
}

5. TurnOn.java

package com.hubberspot.designpatterns.command;
// TurnOn Command is the concrete implementation
// of Command interface also having a Device with it
// which is been set up by the constructor.
// its implemented execute method will call the
// turnOn Button on the remote control and will
// start the television or any other device.
public class TurnOn implements Command {
private Device device;
public TurnOn(Device device) {
this.device = device;
}
@Override
public void execute() {
device.on();
}
}