This unit focuses on two design patterns: Observer and Strategy. We'll use these patterns to enable responsive security and flexible climate control in our smart home system.
Observer Pattern:
Subject
with registration, unregistration, and notification methods (e.g., for a SecuritySystem
).SecurityApp
, SecurityLight
).Strategy Pattern:
TemperatureControlStrategy
for controlling temperature).HeatingStrategy
, CoolingStrategy
).ClimateControl
system).Here is the complete code to implement the Observer pattern:
Python1# Define the Subject class 2class Subject: 3 def __init__(self): 4 self.__observers = [] 5 6 def register_observer(self, observer): 7 self.__observers.append(observer) 8 9 def unregister_observer(self, observer): 10 self.__observers.remove(observer) 11 12 def notify_observers(self): 13 for observer in self.__observers: 14 observer.update(self) 15 16# Define the SecuritySystem class 17class SecuritySystem(Subject): 18 def __init__(self): 19 super().__init__() 20 self.__state = None 21 22 def set_state(self, state): 23 self.__state = state 24 self.notify_observers() 25 26 def get_state(self): 27 return self.__state 28 29# Define the Observer interface 30class Observer: 31 def update(self, subject): 32 pass 33 34# Implement the SecurityApp observer 35class SecurityApp(Observer): 36 def update(self, subject): 37 if isinstance(subject, SecuritySystem): 38 print(f"SecurityApp notified. New state: {subject.get_state()}") 39 40# Implement the SecurityLight observer 41class SecurityLight(Observer): 42 def update(self, subject): 43 if isinstance(subject, SecuritySystem): 44 print(f"SecurityLight activated! State: {subject.get_state()}") 45 46# Example usage 47security_system = SecuritySystem() 48app = SecurityApp() 49light = SecurityLight() 50security_system.register_observer(app) 51security_system.register_observer(light) 52security_system.set_state("Intrusion Detected") 53 54# Expected Output: 55# SecurityApp notified. New state: Intrusion Detected 56# SecurityLight activated! State: Intrusion Detected
The security_system
instance notifies both SecurityApp
and SecurityLight
when the state changes to "Intrusion Detected".
Here is the complete code to implement the Strategy pattern:
Python1# Define the TemperatureControlStrategy interface 2class TemperatureControlStrategy: 3 def control_temperature(self, temperature): 4 pass 5 6# Implement the HeatingStrategy 7class HeatingStrategy(TemperatureControlStrategy): 8 def control_temperature(self, temperature): 9 print(f"Heating to {temperature} degrees.") 10 11# Implement the CoolingStrategy 12class CoolingStrategy(TemperatureControlStrategy): 13 def control_temperature(self, temperature): 14 print(f"Cooling to {temperature} degrees.") 15 16# Define the ClimateControl context 17class ClimateControl: 18 def __init__(self, strategy: TemperatureControlStrategy): 19 self.__strategy = strategy 20 21 def set_strategy(self, strategy: TemperatureControlStrategy): 22 self.__strategy = strategy 23 24 def control_temperature(self, temperature): 25 self.__strategy.control_temperature(temperature) 26 27# Example usage 28climate_control = ClimateControl(HeatingStrategy()) 29climate_control.control_temperature(22) # Expected Output: Heating to 22 degrees. 30 31climate_control.set_strategy(CoolingStrategy()) 32climate_control.control_temperature(18) # Expected Output: Cooling to 18 degrees.
The ClimateControl
instance can switch between HeatingStrategy
and CoolingStrategy
to control the temperature accordingly.
By using the Observer and Strategy patterns, we enhance our smart home system's adaptability and functionality, allowing for responsive security alerts and flexible climate control.