Advanced Java September 27 ,2025

Command Design Pattern in Java

1. Introduction

In many applications, we want to issue requests without knowing exactly who will handle them or how they will be executed. The Command Design Pattern solves this by encapsulating a request into a separate object.

This pattern helps in:

  • Decoupling the sender (who issues the request) from the receiver (who performs the action).
  • Supporting undo/redo operations.
  • Queueing or logging requests.

2. Real-Life Analogy

Think of a remote control:

  • The remote (Invoker) does not know how the TV, fan, or AC actually works.
  • Each button press is a command.
  • The appliance (Receiver) knows how to perform the action.

The remote just sends the command; the appliance executes it.

3. Structure

  • Command Interface → Declares an execute() method.
  • ConcreteCommand → Implements the command, calling methods on the receiver.
  • Receiver → Knows how to perform the actual operation.
  • Invoker → Calls the command without knowing the details.
  • Client → Creates the command and configures the invoker.

4. Custom Java Implementation

Step 1: Command Interface

interface Command {
    void execute();
}

Step 2: Receiver Classes (Devices)

class Light {
    public void turnOn() {
        System.out.println("Light is ON");
    }
    public void turnOff() {
        System.out.println("Light is OFF");
    }
}

class Fan {
    public void start() {
        System.out.println("Fan started");
    }
    public void stop() {
        System.out.println("Fan stopped");
    }
}

Step 3: Concrete Commands

class LightOnCommand implements Command {
    private Light light;
    public LightOnCommand(Light light) {
        this.light = light;
    }
    @Override
    public void execute() {
        light.turnOn();
    }
}

class LightOffCommand implements Command {
    private Light light;
    public LightOffCommand(Light light) {
        this.light = light;
    }
    @Override
    public void execute() {
        light.turnOff();
    }
}

class FanStartCommand implements Command {
    private Fan fan;
    public FanStartCommand(Fan fan) {
        this.fan = fan;
    }
    @Override
    public void execute() {
        fan.start();
    }
}

class FanStopCommand implements Command {
    private Fan fan;
    public FanStopCommand(Fan fan) {
        this.fan = fan;
    }
    @Override
    public void execute() {
        fan.stop();
    }
}

Step 4: Invoker (Remote Control)

class RemoteControl {
    private Command command;

    public void setCommand(Command command) {
        this.command = command;
    }

    public void pressButton() {
        if (command != null) {
            command.execute();
        }
    }
}

Step 5: Client Code

public class CommandPatternDemo {
    public static void main(String[] args) {
        // Receivers
        Light livingRoomLight = new Light();
        Fan ceilingFan = new Fan();

        // Concrete commands
        Command lightOn = new LightOnCommand(livingRoomLight);
        Command lightOff = new LightOffCommand(livingRoomLight);
        Command fanStart = new FanStartCommand(ceilingFan);
        Command fanStop = new FanStopCommand(ceilingFan);

        // Invoker
        RemoteControl remote = new RemoteControl();

        // Execute commands
        System.out.println("Turning ON Light:");
        remote.setCommand(lightOn);
        remote.pressButton();

        System.out.println("\nTurning OFF Light:");
        remote.setCommand(lightOff);
        remote.pressButton();

        System.out.println("\nStarting Fan:");
        remote.setCommand(fanStart);
        remote.pressButton();

        System.out.println("\nStopping Fan:");
        remote.setCommand(fanStop);
        remote.pressButton();
    }
}

5. Output

Turning ON Light:
Light is ON

Turning OFF Light:
Light is OFF

Starting Fan:
Fan started

Stopping Fan:
Fan stopped

6. Benefits

  • Loose coupling: The invoker doesn’t know how the receiver performs the action.
  • Flexibility: Easy to add new commands without changing existing code.
  • Supports undo/redo by keeping track of executed commands.
  • Useful in queues, logs, and macro commands.

7. When to Use

  • When requests need to be parameterized as objects.
  • When you want to support undo/redo functionality.
  • When you need to queue, log, or batch requests.

 

Next- Interpreter Design Pattern in Java

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com