This example demonstrates a Smart Home System where a SmartHomeController manages various independent components like Light, Thermostat, and SecuritySystem.
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();
}
}
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.
Light, Thermostat, SecuritySystem) hides internal details and only provides controlled access via methods like turnOn() and turnOff().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.SmartDevice interface defines common methods (turnOn(), turnOff(), isOn()), ensuring all smart devices follow the same contract.SmartHomeController doesn’t care what type of smart device it manages; as long as the device implements SmartDevice, it works!✅ Loose Coupling – SmartHomeController 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! 🚀