Command design pattern with java real time example

In this post, we will try to explain the importance of the Command design pattern with a java real-time example. The command pattern is one of the behavioural design patterns. behavioural design patterns deal with objects and how they communicate with

command-pattern-demo-real-time-exampleeach other. The command pattern is used when we don’t know the input request and what the will be expected output response.  A real-time ex

ample is the app of amazon or Flipkart. We can order groceries or mobile or anything. it is not fixed to one type of input. Either it is electronics or vegetables or even cloth items. user experience is the same for all types of items. The app works for different types of inputs and processes them and gives different types of output. It encapsulates a request as an object in this pattern. By binding a set of actions on the specific receiver. with this pattern, we can reverse or roll back the transaction like undo. it is widely used in undo and redo operations. It makes code more flexible and adds more commands without disturbing the code. it allows the application to accept the request and without being known of the requestor receiver of the request. Based on the type of command, respective operations will be performed. receiver, invoker, and command will work together to execute the operation. It provides loose coupling.

When to use? Parameterize objects by an action. When you want to support undo operation:  Command’s Execute operation can store state for reversing its effects in the Command itself.

interface Command {

void execute();

}

class OpenFileCommand implements Command {

FileReciever fileReciver;

@Override
public void execute() {

this.fileReciver.openFile();

}

public OpenFileCommand(FileReciever fileReciver) {
this.fileReciver = fileReciver;
}

}

class CloseFileCommand implements Command {
FileReciever fileReciver;

@Override
public void execute() {
this.fileReciver.closeFile();
}

public CloseFileCommand(FileReciever fileReciver) {
this.fileReciver = fileReciver;
}

}




public interface FileReciever {
void openFile();
void closeFile();
}
class WindowsFileReciever implements FileReciever
{

@Override
public void openFile() {
System.out.println("Windows opening file");

}

@Override
public void closeFile() {
System.out.println("Windows closing file"); 
}

}
class LinuxFileReciever implements FileReciever
{

@Override
public void openFile() {
System.out.println("Linux opening file");


}

@Override
public void closeFile() {
System.out.println("Linux closing file"); 

}

}




public class Invoker {

Command command;


public Invoker(Command command) {
this.command = command;
}

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

}



public class CommandDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub

WindowsFileReciever fileReciever = new WindowsFileReciever();
OpenFileCommand command= new OpenFileCommand(fileReciever);
Invoker obj= new Invoker(command);
obj.execute();

FileReciever fileReciever2 = new LinuxFileReciever();
command= new OpenFileCommand(fileReciever2);
Invoker obj2= new Invoker(command);
obj2.execute();

}

}


 

 


Posted

in

by

Tags:

Comments

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started