Java Code Example: Smart Home Automation System

This example demonstrates a Smart Home System where a SmartHomeController manages various independent components like Light, Thermostat, and SecuritySystem.


Java Code Example: Smart Home Automation System

import java.util.ArrayList;
import java.util.List;

// Interface to define common behavior for all smart devices
interface SmartDevice {
    void turnOn();
    void turnOff();
    boolean isOn();
}

// Smart Light class
class Light implements SmartDevice {
    private boolean status = false;

    @Override
    public void turnOn() {
        status = true;
        System.out.println("Light turned ON.");
    }

    @Override
    public void turnOff() {
        status = false;
        System.out.println("Light turned OFF.");
    }

    @Override
    public boolean isOn() {
        return status;
    }
}

// Smart Thermostat class
class Thermostat implements SmartDevice {
    private boolean status = false;
    private double temperature = 22.0; // Default temperature

    @Override
    public void turnOn() {
        status = true;
        System.out.println("Thermostat turned ON. Current temperature: " + temperature + "°C");
    }

    @Override
    public void turnOff() {
        status = false;
        System.out.println("Thermostat turned OFF.");
    }

    public void setTemperature(double temp) {
        if (status) {
            this.temperature = temp;
            System.out.println("Thermostat set to " + temp + "°C");
        } else {
            System.out.println("Turn on the thermostat first!");
        }
    }

    @Override
    public boolean isOn() {
        return status;
    }
}

// Smart Security System class
class SecuritySystem implements SmartDevice {
    private boolean status = false;

    @Override
    public void turnOn() {
        status = true;
        System.out.println("Security System ACTIVATED.");
    }

    @Override
    public void turnOff() {
        status = false;
        System.out.println("Security System DEACTIVATED.");
    }

    @Override
    public boolean isOn() {
        return status;
    }
}

// Smart Home Controller (Manages multiple smart devices)
class SmartHomeController {
    private List<SmartDevice> devices = new ArrayList<>();

    // Method to add a new device
    public void addDevice(SmartDevice device) {
        devices.add(device);
        System.out.println("Device added to Smart Home Controller.");
    }

    // Method to turn on all devices
    public void activateAll() {
        System.out.println("Activating all smart devices...");
        for (SmartDevice device : devices) {
            device.turnOn();
        }
    }

    // Method to turn off all devices
    public void deactivateAll() {
        System.out.println("Deactivating all smart devices...");
        for (SmartDevice device : devices) {
            device.turnOff();
        }
    }
}

// Main Class
public class SmartHomeSystem {
    public static void main(String[] args) {
        // Creating Smart Devices
        Light livingRoomLight = new Light();
        Thermostat homeThermostat = new Thermostat();
        SecuritySystem homeSecurity = new SecuritySystem();

        // Creating Smart Home Controller
        SmartHomeController controller = new SmartHomeController();

        // Adding devices to the controller
        controller.addDevice(livingRoomLight);
        controller.addDevice(homeThermostat);
        controller.addDevice(homeSecurity);

        // Activating all smart devices
        controller.activateAll();

        // Adjusting thermostat temperature
        homeThermostat.setTemperature(24.5);

        // Deactivating all smart devices
        controller.deactivateAll();
    }
}

Expected Output:

Device added to Smart Home Controller.
Device added to Smart Home Controller.
Device added to Smart Home Controller.
Activating all smart devices...
Light turned ON.
Thermostat turned ON. Current temperature: 22.0°C
Security System ACTIVATED.
Thermostat set to 24.5°C
Deactivating all smart devices...
Light turned OFF.
Thermostat turned OFF.
Security System DEACTIVATED.

Code Explanation:

1️⃣ Encapsulation

  • Each smart device (Light, Thermostat, SecuritySystem) hides internal details and only provides controlled access via methods like turnOn() and turnOff().

2️⃣ Composition Instead of Inheritance

  • Instead of making Light, Thermostat, and SecuritySystem subclasses of a common parent, we use composition by adding them to SmartHomeController dynamically.
  • SmartHomeController doesn’t need to extend another class, making the system modular and flexible.

3️⃣ Interfaces & Dependency Injection

  • The SmartDevice interface defines common methods (turnOn(), turnOff(), isOn()), ensuring all smart devices follow the same contract.
  • The SmartHomeController doesn’t care what type of smart device it manages; as long as the device implements SmartDevice, it works!
  • This decouples components and allows easy addition of new devices in the future.

Why Use This Approach?

Loose CouplingSmartHomeController doesn’t depend on specific implementations of Light, Thermostat, or SecuritySystem.
Scalability – New smart devices can be added without modifying existing code.
Reusability – The SmartDevice interface allows code reuse across different devices.
Modularity – Each class performs a single responsibility, making the system easier to maintain.

This Java example demonstrates advanced object-oriented design by leveraging encapsulation, interfaces, and composition, resulting in a scalable and flexible smart home system—without using inheritance or polymorphism! 🚀