Building the Smart Home Automation and Lighting System

As we continue our smart home system project, this unit will focus on two design patterns: the Command pattern and the Decorator pattern. These patterns will help us create a flexible and expandable system for automation and lighting control.

Quick Summary
  1. Basic Setup:

    • Abstract Device: Define an abstract class Device and derive specific device classes (Light, Fan) from it.
    • Factory Method: Implement factory classes to generate instances of these devices.
  2. Command Pattern:

    • Purpose: Encapsulates a request as an object, allowing for parameterization, queuing, logging of requests, and support for undoable operations.
    • Components:
      • Define a ICommand interface and concrete command classes (LightOnCommand, LightOffCommand).
      • Implement a RemoteControl class to execute commands.
  3. Decorator Pattern:

    • Purpose: Adds additional functionalities dynamically to existing objects without altering their structure.
    • Components:
      • Define a decorator class (ColoredLight) to add color functionalities to a Light device.

Let’s move forward and start implementing these patterns.

Defining Smart Home Devices

Before diving into the design patterns, we need to define the devices we'll be working with. We'll start by defining an abstract class Device. Then, create specific device classes Light and Fan that inherit from Device.

// Abstract Product
public abstract class Device
{
    public abstract void On();
    public abstract void Off();
}

// Concrete Device Light
public class Light : Device
{
    public override void On() => Console.WriteLine("Light is on.");
    public override void Off() => Console.WriteLine("Light is off.");
}

// Concrete Device Fan
public class Fan : Device
{
    private int speed;
    public override void On() => Console.WriteLine("Fan is on.");
    public override void Off() => Console.WriteLine("Fan is off.");
    public void SetSpeed(int speed)
    {
        this.speed = speed;
        Console.WriteLine($"Fan speed set to {speed}.");
    }
}
Factory Method for Devices

Next, implement a factory class to generate instances of these devices.

public abstract class DeviceFactory
{
    public abstract Device CreateDevice();
}

public class LightFactory : DeviceFactory
{
    public override Device CreateDevice() => new Light();
}

public class FanFactory : DeviceFactory
{
    public override Device CreateDevice() => new Fan();
}
Command Interface and Concrete Commands

Now, we'll use the Command pattern to create flexible automation commands. For that, let's define a ICommand interface and the concrete command classes.

public interface ICommand
{
    void Execute();
}

public class LightOnCommand : ICommand
{
    private Device device;

    public LightOnCommand(Device device)
    {
        this.device = device;
    }

    public void Execute() => device.On();
}

public class LightOffCommand : ICommand
{
    private Device device;

    public LightOffCommand(Device device)
    {
        this.device = device;
    }

    public void Execute() => device.Off();
}
Remote Control Implementation

Let's also implement the remote control to execute commands.

public class RemoteControl
{
    private ICommand? command;

    public void SetCommand(ICommand command)
    {
        this.command = command;
    }

    public void PressButton()
    {
        if (command != null)
        {
            command.Execute();
        }
    }
}
Using the Command Pattern

Now, let's use the RemoteControl class in the Main program.

public class Program
{
    static void Main()
    {
        // Create a light factory to instantiate Light devices
        DeviceFactory lightFactory = new LightFactory();
        Device light = lightFactory.CreateDevice();

        // Create command instances for turning the light on and off
        ICommand lightOn = new LightOnCommand(light);
        ICommand lightOff = new LightOffCommand(light);

        // Set commands in the remote control
        RemoteControl remote = new RemoteControl();
        remote.SetCommand(lightOn);
        
        // Simulate pressing the button on the remote control
        remote.PressButton(); // Output: Light is on.
        remote.SetCommand(lightOff);
        remote.PressButton(); // Output: Light is off.
    }
}
Decorator Implementation

Next, we'll apply the Decorator pattern to enhance our lighting system. We'll define a decorator class ColoredLight to add color functionalities to a Light device.

public class ColoredLight : Device
{
    private Device light;
    private string color;

    public ColoredLight(Device light, string color)
    {
        this.light = light;
        this.color = color;
    }

    public override void On()
    {
        light.On();
        Console.WriteLine($"Light color changed to {color}.");
    }

    public override void Off()
    {
        light.Off();
    }
}
Using the Decorator Pattern

Finally, we can use the decorator to add functionalities to devices.

public class Program
{
    static void Main()
    {
        // Create a light factory
        DeviceFactory lightFactory = new LightFactory();
        Device light = lightFactory.CreateDevice();
        
        // Decorate the light with red color
        Device redLight = new ColoredLight(light, "Red");
        // Ensure each ColoredLight has its own base device
        Device blueLight = new ColoredLight(lightFactory.CreateDevice(), "Blue");

        redLight.On(); // Output: Light is on. Light color changed to Red.
        redLight.Off(); // Output: Light is off.
        blueLight.On(); // Output: Light is on. Light color changed to Blue.
        blueLight.Off(); // Output: Light is off.
    }
}
Conclusion

By using the Command and Decorator design patterns, we have crafted a smart home system that is modular and flexible. These patterns enable you to extend functionality and dynamically add features without modifying the core structure.

Now it's your turn—get hands-on by implementing these patterns yourself. Let's bring your smart home system to life!

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