CodesJava

Easy learning with example program codes

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.codesjava;
 
public interface FileSystem {
	public void readFile(); 
	public void writeFile();
}

WindowsFileUtility.java

package com.codesjava;
 
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.codesjava;
 
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.codesjava;
 
public interface Command {
	public void execute(); 
}

ReadCommand.java

package com.codesjava;
 
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.codesjava;
 
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.codesjava;
 
public class FileInvoker {
	public Command command;
 
	public FileInvoker(Command c){
		this.command=c;
	}
 
	public void execute(){
		this.command.execute();
	}
}

FileSystemUtil.java

package com.codesjava;
 
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.codesjava;
 
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.
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki