Category : Design Patterns | Sub Category : Design patterns | By Prasad Bonam Last updated: 2023-07-08 15:28:26 Viewed : 663
Command line design pattern in java:
In Java, the Command Line design pattern is not specific to the language itself, but rather a general software design pattern that can be applied to any programming language. The Command Line design pattern is also known as the Command pattern and is categorized as a behavioral design pattern.
The Command pattern is used to encapsulate a request or an operation as an object, allowing the request to be parameterized with different arguments. It enables the separation of the sender of a request from the object that performs the action, providing a flexible and decoupled way to invoke specific operations.
Here is an example of how you can implement the Command Line design pattern in Java:
javapublic interface Command {
void execute();
}
javapublic class OpenFileCommand implements Command {
private final String filePath;
public OpenFileCommand(String filePath) {
this.filePath = filePath;
}
public void execute() {
// Code to open the file using the provided filePath
System.out.println("Opening file: " + filePath)
;
}
}
public class SaveFileCommand implements Command {
private final String filePath;
public SaveFileCommand(String filePath) {
this.filePath = filePath;
}
public void execute() {
// Code to save the file using the provided filePath
System.out.println("Saving file: " + filePath);
}
}
javapublic class CommandInvoker {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void executeCommand() {
command.execute();
}
}
java// Command interface
public interface Command {
void execute();
}
// Concrete command classes
public class OpenCommand implements Command {
private File file;
public OpenCommand(File file) {
this.file = file;
}
@Override
public void execute() {
// Logic to open the file
System.out.println("Opening file: " + file.getName());
}
}
public class SaveCommand implements Command {
private File file;
public SaveCommand(File file) {
this.file = file;
}
@Override
public void execute() {
// Logic to save the file
System.out.println("Saving file: " + file.getName());
}
}
// Invoker class
public class CommandInvoker {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void executeCommand() {
command.execute();
}
}
// Client code
public class Main {
public static void main(String[] args) {
// Create the invoker
CommandInvoker invoker = new CommandInvoker();
// Create the commands
File file = new File("example.txt");
Command openCommand = new OpenCommand(file);
Command saveCommand = new SaveCommand(file);
// Set and execute the open command
invoker.setCommand(openCommand);
invoker.executeCommand();
// Set and execute the save command
invoker.setCommand(saveCommand);
invoker.executeCommand();
}
}
In this example, the user input determines the type of command to execute. The CommandInvoker
class sets the appropriate command and executes it using the executeCommand
method. This allows for the flexibility of adding new commands without modifying the invoker or the client code.
Note that this is a simplified example, and in real-world scenarios, you may have more complex command structures, command parameters, and the use of additional design patterns to enhance the flexibility and extensibility of the pattern.