Design Pattern In Java Part XVIII Command Design Pattern

Posted By : Aftab Alam | 25-Dec-2018

1. As I have written the last blog on Bridge Design Patten and this blog is going to be on Command Design Pattern in which an object encapsulates all information needed to perform an action later on.

     1.1. Purpose of Design Pattern:- The purpose of design pattern, as per Gang of Four(GoF), to solve recurring problems to design flexible and reusable object-oriented Softwares.

     1.2. Behavioural Design Pattern:- In Software Engineering, the behavioral design pattern is design patterns that identify common communication patterns between objects and realize these patterns.

     1.3. Command Design Pattern:- The Command Design Pattern falls in the category of the behavioral design pattern. In this design pattern, an object encapsulates all information needed to perform an action later on.

There are four terms which are always associated with command design pattern are command, receiver, invoker, and client. Receiver object is known to command object and a method of the receiver object is invoked by command object. An invoker object knows how to execute a command 

2. Requirements:- To executed this program, pre-requisites have been listed below out.

    2.1. Ubuntu 14.04 LTS

    2.2. Ubuntu gedit editor

    2.3. java version "1.8.0_181" 

3. Implementation:- It has been divided into multiple steps.

   3.1. Code Snippet:-  The code snippets have been provided for the Command Design Pattern.

         3.1.1. One interface named as ElectronicDevice consists of some methods which can be implemented by implementing the class.

     public interface ElectronicDevice{
	void switchOn();
	void switchOff();
	void volumeUp();
	void volumeDown();
     }

        3.1.2. The Television class implements ElectronicDevice interface and implements its methods as per need.

     public class Television implements ElectronicDevice {

	private int volumeLevel=0;

	public void switchOn(){
		System.out.println("TV is on");
	}

	public void switchOff(){
		System.out.println("TV is off");
	}

	public void volumeUp(){
		volumeLevel++;
		System.out.println("Volume Level is: "+volumeLevel);
	}

	public void volumeDown(){
		volumeLevel--;
		System.out.println("Volume Level is: "+volumeLevel);
	}
    }

       3.1.3. One more interface is created named as Command which has only one method.

     public interface Command {
	void execute();
     }

       3.1.4. Multiple classes have been created which implement Command interface and provides an implementation for its method.

         3.1.4.1. The TelevisionSwitchOn implements Command interface and provides its method implementation. The constructor of TelevisionSwitchOn receives an instance of a class which implements ElectronicDevice interface and invokes a method defined in a class for which it receives an instance as a parameter.

     public class TelevisionSwitchOn implements Command {

	private ElectronicDevice electronicDevice;	

	public TelevisionSwitchOn(ElectronicDevice electronicDevice){
		this.electronicDevice=electronicDevice;
	}

	public void execute(){
		this.electronicDevice.switchOn();
	}
    }

         3.1.4.2. The TelevisionVolumeUp implements Command interface and provides its method implementation. The constructor of TelevisionVolumeUp receives an instance of a class which implements ElectronicDevice interface and invokes a method defined in a class for which it receives an instance as a parameter.

    public class TelevisionVolumeUp implements Command {

	private ElectronicDevice electronicDevice;	

	public TelevisionVolumeUp(ElectronicDevice electronicDevice){
		this.electronicDevice=electronicDevice;
	}

	public void execute(){
		this.electronicDevice.volumeUp();
	}
    }

        3.1.4.3. The TelevisionVolumeDown implements Command interface and provides its method implementation. The constructor of TelevisionVolumeDown receives an instance of a class which implements ElectronicDevice interface and invokes a method defined in a class for which it receives an instance as a parameter.

    public class TelevisionVolumeDown implements Command {

	private ElectronicDevice electronicDevice;	

	public TelevisionVolumeDown(ElectronicDevice electronicDevice){
		this.electronicDevice=electronicDevice;
	}

	public void execute(){
		this.electronicDevice.volumeDown();
	}
   }

     3.1.4.4. The TelevisionSwitchOff implements Command interface and provides its method implementation. The constructor of TelevisionSwitchOff receives an instance of a class which implements ElectronicDevice interface and invokes a method defined in a class for which it receives an instance as a parameter.

   public class TelevisionSwitchOff implements Command {

	private ElectronicDevice electronicDevice;	

	public TelevisionSwitchOff(ElectronicDevice electronicDevice){
		this.electronicDevice=electronicDevice;
	}

	public void execute(){
		this.electronicDevice.switchOff();
	}
   }

     3.1.5. The TelevisionDevice class is created which receives, in the constructor as a parameter, an instance of a class that implements the command interface. It defines a method which calls a method of a class whose instance has been received as a parameter.

   public class TelevisionDevice {
	
	private Command command;	
	
	public TelevisionDevice(Command command){
		this.command=command;
	}

	public void press(){
		this.command.execute();
	}
   }

    3.1.6. The CommandDesignPattern class demonstrates the command design pattern. It creates an instance of Television class which is passed as a parameter in the constructor of all ElectronicDevice's implementation classes whose instances are assigned into reference variables of Command interface then these instances are passed as a parameter in the constructor of TelevisionDevice class which are used to invoke a press method.

    public class CommandDesignPattern{

	public static void main(String a[]){
	
		ElectronicDevice electronicDevice=new Television();
		
		/* Switch On the television */
		Command commandSwitchOn=new TelevisionSwitchOn(electronicDevice);
		TelevisionDevice televisionDeviceSwitchOn=new TelevisionDevice(commandSwitchOn);
		televisionDeviceSwitchOn.press();

		/* Increase the volume */
		Command commandVolumeUp=new TelevisionVolumeUp(electronicDevice);
		TelevisionDevice televisionDeviceVolumeUp=new TelevisionDevice(commandVolumeUp);
		televisionDeviceVolumeUp.press();

		/* Decrease the volume */
		Command commandVolumeDown=new TelevisionVolumeDown(electronicDevice);
		TelevisionDevice televisionDeviceVolumeDown=new TelevisionDevice(commandVolumeDown);
		televisionDeviceVolumeDown.press();
	
		/* Switch Off the television */
		Command commandSwitchOff=new TelevisionSwitchOff(electronicDevice);
		TelevisionDevice televisionDeviceSwitchOff=new TelevisionDevice(commandSwitchOff);
		televisionDeviceSwitchOff.press();
	}
   }

   3.2. Flow Diagram:- Listed below image is trying to depict Bridge Design Pattern flow.

   3.3. Commands:- To compile and execute this program, listed below commands are used on ubuntu terminal.

     3.3.1. "javac CommandDesignPattern.java" to compile the java program

     3.3.2. "java CommandDesignPattern" to execute the program which produces the result shown in below screenshot.

   3.4. Output:- When the above program is executed then It produces an output which is shown in the screenshot.

3.5. References:- Listed below out are some references.

3.5.1. https://en.wikipedia.org/wiki/Command_pattern
3.5.2. https://www.youtube.com/watch?v=7Pj5kAhVBlg

4. Conclusion:- Implementing (hard-wiring) a request directly into a class is inflexible because it couples the class to a particular request at compile-time, which makes it impossible to specify a request at run-time.

About Author

Author Image
Aftab Alam

Aftab has worked on multiple technologies in front-end as well as in back-end.

Request for Proposal

Name is required

Comment is required

Sending message..