Pin It

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();

}




2. Television.java
package com.hubberspot.designpatterns.command;

// Lets create a new concrete device say 
// Television which will implement all the 
// methods of Device interface.

public class Television implements Device {

 private int volume = 0;
 private int channel = 0;

 @Override
 public void on() {

  System.out.println("Television is On");

 }

 @Override
 public void off() {

  System.out.println("Television is Off");

 }

 @Override
 public void volumeUp() {

  volume++;
  System.out.println("Volume : " + volume);

 }

 @Override
 public void volumeDown() {

  volume--;
  System.out.println("Volume : " + volume);

 }

 @Override
 public void channelUp() {

  channel++;
  System.out.println("Channel : " + channel);

 }

 @Override
 public void channelDown() {

  channel--;
  System.out.println("Channel : " + channel);

 }

}




3. Radio.java
package com.hubberspot.designpatterns.command;

// This would be another concrete device
// other than Television
public class Radio implements Device {

 private int volume = 0;
 private int channel = 0;

 @Override
 public void on() {

  System.out.println("Radio is On");

 }

 @Override
 public void off() {

  System.out.println("Radio is Off");

 }

 @Override
 public void volumeUp() {

  volume++;
  System.out.println("Volume : " + volume);

 }

 @Override
 public void volumeDown() {

  volume--;
  System.out.println("Volume : " + volume);

 }

 @Override
 public void channelUp() {

  channel++;
  System.out.println("Channel : " + channel);

 }

 @Override
 public void channelDown() {

  channel--;
  System.out.println("Channel : " + channel);

 }


}



4. Command.java
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();

 }

}



6. TurnOff.java
package com.hubberspot.designpatterns.command;

// Same as TurnOn Command
public class TurnOff implements Command {

 private Device device;

 public TurnOff(Device device) {

  this.device = device;

 }

 @Override
 public void execute() {

  device.off();

 }


}



7. VolumeUp.java
package com.hubberspot.designpatterns.command;

//Same as TurnOn Command
public class VolumeUp implements Command {

 private Device device;

 public VolumeUp(Device device) {

  this.device = device;

 }

 @Override
 public void execute() {

  device.volumeUp();

 }


}



8. VolumeDown.java
package com.hubberspot.designpatterns.command;

//Same as TurnOn Command
public class VolumeDown implements Command {

 private Device device;

 public VolumeDown(Device device) {

  this.device = device;

 }

 @Override
 public void execute() {

  device.volumeDown();

 }


}



9. ChannelUp.java
package com.hubberspot.designpatterns.command;

//Same as TurnOn Command
public class ChannelUp implements Command {

 private Device device;

 public ChannelUp(Device device) {

  this.device = device;

 }

 @Override
 public void execute() {

  device.channelUp();

 }


}



10. ChannelDown.java
package com.hubberspot.designpatterns.command;

//Same as TurnOn Command
public class ChannelDown implements Command {

 private Device device;

 public ChannelDown(Device device) {

  this.device = device;

 }

 @Override
 public void execute() {

  device.channelDown();

 }


}




11. RemoteControl.java
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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot