Command Design Pattern in Scala

1. Definition

The Command design pattern encapsulates a request as an object, allowing users to parameterize clients with different requests, queue requests, and support operations that can be undone. It separates the object that invokes the operation from the object that knows how to perform the operation.

2. Problem Statement

Consider a scenario where you have a GUI toolkit and you want to allow users to customize the behavior of buttons, menu items, etc. Directly coding each behavior in each GUI component can make the system hard to maintain and inflexible to changes.

3. Solution

Instead of hard-coding operations, turn the operation into an object. This object can be passed, stored, and inserted wherever needed. It provides a separation between the receiver of the command and the invoker, increasing the flexibility of the system.

4. Real-World Use Cases

1. GUI buttons and menu items in software applications.

2. Macro recording in software programs.

3. Task scheduling systems.

5. Implementation Steps

1. Define a Command interface with an execute method.

2. Create one or more derived classes that encapsulate some subset of the receiver's functionality.

3. Instantiate a command object for each deferred execution request.

4. Pass the Command from the invoker to the receiver.

6. Implementation in Scala Programming

// Command Trait
trait Command {
  def execute(): Unit
// Receiver
class Light {
  def turnOn(): Unit = println("Light is ON")
  def turnOff(): Unit = println("Light is OFF")
// Concrete Command
class TurnOnCommand(light: Light) extends Command {
  def execute(): Unit = light.turnOn()
class TurnOffCommand(light: Light) extends Command {
  def execute(): Unit = light.turnOff()
// Invoker
class RemoteControl {
  private var command: Command = _
  def setCommand(command: Command): Unit = this.command = command
  def pressButton(): Unit = command.execute()
// Client
object CommandClient extends App {
  val light = new Light
  val remote = new RemoteControl
  remote.setCommand(new TurnOnCommand(light))
  remote.setCommand(new TurnOffCommand(light))


Light is ON
Light is OFF


1. A Command trait is defined that represents an action.

2. The Light class (Receiver) knows how to perform the operation.

3. Concrete commands (TurnOnCommand and TurnOffCommand) encapsulate actions as objects.

4. The RemoteControl class (Invoker) knows how to execute a command, but it is decoupled from the receiver.

5. The client sets up the relationship between the invoker and the command, and then triggers the command via the invoker.

7. When to use?

Use the Command pattern when:

1. You need to parameterize objects with operations.

2. You want to queue operations, schedule their execution, or execute them remotely.

3. You need to support undoable operations.