Building the Smart Home Automation and Lighting System with JavaScript Design Patterns

Building the Smart Home Automation and Lighting System

In this unit, we'll implement two essential design patterns: the Command pattern and the Decorator pattern, aimed at enhancing our smart home automation and lighting system. These patterns help create a more flexible and expandable system for automation and lighting control.

Quick Summary

  1. Command Pattern:

    • Purpose: Encapsulates a request as an object, enabling parameterization, queuing, logging, and support for undoable operations.
    • Steps:
      • Define a command class with an execute method.
      • Create concrete command classes (LightOnCommand, LightOffCommand) that implement this method and perform specific actions.
      • Implement a receiver class (Light) with methods to turn the light on and off.
      • Use an invoker class (RemoteControl) to store and execute commands.
  2. Decorator Pattern:

    • Purpose: Dynamically adds additional functionality to objects without altering their structure.
    • Steps:
      • Define a component class (Light).
      • Create a base decorator class (LightDecorator) that implements the component interface and holds a reference to a component object.
      • Develop concrete decorators (ColorChangeDecorator) to extend functionalities of the basic component.

Implementing the Command Pattern

Using the Command pattern, we define our commands and receivers, then utilize an invoker to store and execute commands. This enables flexible and parameterizable control over our smart home system.

Code Implementation for Command Pattern

Here is the complete code for implementing the Command pattern using JavaScript:

// Command interface
class Command {
    execute() {
        throw new Error('Execute method must be overridden.');
    }
}

// Concrete command classes for turning the light on and off
class LightOnCommand extends Command {
    constructor(light) {
        super();
        this.light = light;
    }

    execute() {
        this.light.turnOn();
    }
}

class LightOffCommand extends Command {
    constructor(light) {
        super();
        this.light = light;
    }

    execute() {
        this.light.turnOff();
    }
}

// Receiver class encapsulating the light functionalities
class Light {
    turnOn() {
        console.log("The light is on");
    }

    turnOff() {
        console.log("The light is off");
    }
}

// Invoker class to store and execute commands
class RemoteControl {
    constructor() {
        this.slots = {};
    }

    setCommand(commandName, command) {
        this.slots[commandName] = command;
    }

    pressButton(commandName) {
        if (this.slots[commandName]) {
            this.slots[commandName].execute();
        } else {
            console.log(`No command found for ${commandName}`);
        }
    }
}

// Example of setting up and using the command pattern
const livingRoomLight = new Light();

const lightOnCommand = new LightOnCommand(livingRoomLight);
const lightOffCommand = new LightOffCommand(livingRoomLight);

const remoteControl = new RemoteControl();

remoteControl.setCommand("light_on", lightOnCommand);
remoteControl.setCommand("light_off", lightOffCommand);

remoteControl.pressButton("light_on");  // Expected Output: The light is on
remoteControl.pressButton("light_off"); // Expected Output: The light is off
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal