Command design pattern Java example

Brandon 131 Published: 11/14/2024

Command design pattern Java example

Here's an example of the Command Design Pattern in Java:

Command Pattern Overview

The Command pattern is a behavioral design pattern that enables you to encapsulate a request or action as a separate object (the command) and execute it independently from its originator.

Java Example: Remote Control System

Let's say we're building a remote control system for controlling devices such as TV, lights, and air conditioners. We want to make sure the commands sent by each device are executed in a consistent manner, regardless of where they come from (e.g., button presses or voice assistants).

// Command interface

interface RemoteControlCommand {

void execute();

}

// Concrete command classes

class TurnOnTVCommand implements RemoteControlCommand {

private TV tv;

public TurnOnTVCommand(TV tv) {

this.tv = tv;

}

@Override

public void execute() {

tv.turnOn();

}

}

class IncreaseVolumeCommand implements RemoteControlCommand {

private TV tv;

public IncreaseVolumeCommand(TV tv) {

this.tv = tv;

}

@Override

public void execute() {

tv.increaseVolume();

}

}

// Receiver classes

class TV {

public void turnOn() {

System.out.println("TV turned on");

}

public void increaseVolume() {

System.out.println("TV volume increased");

}

}

class Light {

public void turnOn() {

System.out.println("Light turned on");

}

public void turnOff() {

System.out.println("Light turned off");

}

}

// Invoker class (Remote Control)

class RemoteControl {

private Command command;

public void setCommand(Command command) {

this.command = command;

}

public void pressButton() {

command.execute();

}

}

public class Main {

public static void main(String[] args) {

TV tv = new TV();

Light light = new Light();

TurnOnTVCommand turnOnTvCmd = new TurnOnTVCommand(tv);

IncreaseVolumeCommand increaseVolCmd = new IncreaseVolumeCommand(tv);

TurnOffLightCommand turnOffLightCmd = new TurnOffLightCommand(light);

RemoteControl remote = new RemoteControl();

// Set the command for TV

remote.setCommand(turnOnTvCmd);

remote.pressButton(); // Output: TV turned on

// Set the command for TV volume

remote.setCommand(increaseVolCmd);

remote.pressButton(); // Output: TV volume increased

// Set the command for Light

remote.setCommand(turnOffLightCmd);

remote.pressButton(); // Output: Light turned off

}

}

How it Works

In this example:

We define an interface RemoteControlCommand that represents a command that can be executed. We create concrete command classes (TurnOnTVCommand, IncreaseVolumeCommand, and TurnOffLightCommand) that implement the RemoteControlCommand interface. Each command class has its own specific behavior (e.g., turning on a TV, increasing the volume of a TV, or turning off a light). We define receiver classes (TV and Light) that are responsible for performing the actual actions (e.g., turning on/off a device). We create an invoker class (RemoteControl) that sets and executes commands.

By using the Command pattern, we decouple the command sender (the button press) from the command receiver (the TV or light), which makes it easier to add new devices or modify existing ones without affecting the rest of the system.

How to use Java command prompt?

I'd be happy to help! However, I must correct you - there is no such thing as a "Java command prompt." The Java programming language is used for developing software, and it doesn't have its own built-in command-line interface or shell.

What you're probably thinking of is the Java Development Kit (JDK) that comes with the Java Runtime Environment (JRE). The JDK includes various tools for compiling, running, and debugging Java programs. One such tool is called javac (short for "Java compiler"), which can be used from a command-line interface.

To use javac from the command line, you'll need to:

Open your operating system's terminal or command prompt. Navigate to the directory where your Java source files are located using the cd command. Use javac to compile your Java program by typing the following command: javac YourProgram.java This will generate a .class file with the same name as your Java source file (e.g., YourProgram.class). To run your Java program, use the java command followed by the class name: java YourProgram

Here's an example of how this might work:

Suppose you have a Java program called HelloWorld.java in a directory called C:JavaProjects. You can compile it using the following command:

cd C:JavaProjects

javac HelloWorld.java

This will generate a file called HelloWorld.class in the same directory. To run the program, you'd use the following command:

java HelloWorld

This would print "Hello, world!" to the console.

In addition to javac, other useful tools that come with the JDK include:

javadoc: generates HTML documentation for your Java programs. jar: creates a JAR (Java Archive) file containing your compiled Java classes and any dependent libraries. jps and jstack: tools for monitoring and debugging Java processes.

I hope this helps! Let me know if you have any further questions.