Java command design pattern

Command simply refers to the ability to use or control something.

Java command design pattern comes under behavioural design patterns. According to the GoF, Command design pattern states that encapsulate a request under an object as a command and pass it to invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command

Let’s discuss command design pattern with below example. We are taking here an example of file system. We have a Fili utility class which contains two methods to read and write files. Our file system should support multiple operating systems like windows, linux etc.

Example

FileSystem.java

package com.w3spoint;
 
public interface FileSystem {
	public void readFile(); 
	public void writeFile();
}

WindowsFileUtility.java

package com.w3spoint;
 
public class WindowsFileUtility implements FileSystem {
	@Override
	public void readFile() {
		System.out.println("Read file in Windos OS.");	
	}
 
	@Override
	public void writeFile() {
		System.out.println("Write file in Windos OS.");		
	}	 	
}

LinuxFileUtility.java

package com.w3spoint;
 
public class LinuxFileUtility implements FileSystem {
	@Override
	public void readFile() {
		System.out.println("Read file in Linux OS.");	
	}
 
	@Override
	public void writeFile() {
		System.out.println("Write file in Linux OS.");		
	}	 	
}

Command.java

package com.w3spoint;
 
public interface Command {
	public void execute(); 
}

ReadCommand.java

package com.w3spoint;
 
public class ReadCommand implements Command {
	private FileSystem fileSystem;
 
	public ReadCommand(FileSystem fileSystem){
		this.fileSystem=fileSystem;
	}
	@Override
	public void execute() {
		this.fileSystem.readFile();
	}	  	
}

WriteCommand.java

package com.w3spoint;
 
public class WriteCommand implements Command {
	private FileSystem fileSystem;
 
	public WriteCommand(FileSystem fileSystem){
		this.fileSystem=fileSystem;
	}
	@Override
	public void execute() {
		this.fileSystem.writeFile();
	}	  	
}

FileInvoker.java

package com.w3spoint;
 
public class FileInvoker {
	public Command command;
 
	public FileInvoker(Command c){
		this.command=c;
	}
 
	public void execute(){
		this.command.execute();
	}
}

FileSystemUtil.java

package com.w3spoint;
 
public class FileSystemUtil {
	public static FileSystem getUnderlyingFileSystem(){
		 String operatingSystem = System.getProperty("os.name");
		 System.out.println("Underlying OS is:"+operatingSystem);
		 if(operatingSystem.contains("Windows")){
			 return new WindowsFileUtility();
		 }else{
			 return new LinuxFileUtility();
		 }
	}  	
}

CommandPatternTest.java

package com.w3spoint;
 
public class CommandPatternTest {
	public static void main(String args[]){
		FileSystem fs = FileSystemUtil.getUnderlyingFileSystem();
		ReadCommand readCommand = new ReadCommand(fs);
		FileInvoker file = new FileInvoker(readCommand);
		file.execute();
 
		WriteCommand writeCommand = new WriteCommand(fs);
		file = new FileInvoker(writeCommand);
		file.execute();
	}
}

Output

Underlying OS is:Windows 7
Read file in Windos OS.
Write file in Windos OS.
Please follow and like us:
Content Protection by DMCA.com