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:
- Purpose: Allows an object (observer) to watch another (subject) and get notified of changes.
- Steps:
- Define a
Subject
with registration, unregistration, and notification methods (e.g., for aSecuritySystem
). - Implement observers that react to changes (e.g.,
SecurityApp
,SecurityLight
).
- Define a
-
Strategy Pattern:
- Purpose: Defines a family of algorithms and makes them interchangeable.
- Steps:
- Define a strategy interface with a method for the desired behavior (e.g.,
TemperatureControlStrategy
for controlling temperature). - Implement specific strategies with concrete behavior (e.g.,
HeatingStrategy
,CoolingStrategy
). - Create a context to use and switch strategies (e.g., a
ClimateControl
system).
- Define a strategy interface with a method for the desired behavior (e.g.,
Here is the complete code to implement the Observer pattern using JavaScript:
JavaScript1// Define the Subject class 2class Subject { 3 constructor() { 4 this._observers = []; 5 } 6 7 registerObserver(observer) { 8 this._observers.push(observer); 9 } 10 11 unregisterObserver(observer) { 12 this._observers = this._observers.filter(obs => obs !== observer); 13 } 14 15 notifyObservers() { 16 this._observers.forEach(observer => observer.update(this)); 17 } 18} 19 20// Define the SecuritySystem class 21class SecuritySystem extends Subject { 22 constructor() { 23 super(); 24 this._state = null; 25 } 26 27 setState(state) { 28 this._state = state; 29 this.notifyObservers(); 30 } 31 32 getState() { 33 return this._state; 34 } 35} 36 37// Define the Observer interface 38class Observer { 39 update(subject) { 40 throw new Error('Observer update method must be implemented.'); 41 } 42} 43 44// Implement the SecurityApp observer 45class SecurityApp extends Observer { 46 update(subject) { 47 if (subject instanceof SecuritySystem) { 48 console.log(`SecurityApp notified. New state: ${subject.getState()}`); 49 } 50 } 51} 52 53// Implement the SecurityLight observer 54class SecurityLight extends Observer { 55 update(subject) { 56 if (subject instanceof SecuritySystem) { 57 console.log(`SecurityLight activated! State: ${subject.getState()}`); 58 } 59 } 60} 61 62// Example usage 63const securitySystem = new SecuritySystem(); 64const app = new SecurityApp(); 65const light = new SecurityLight(); 66securitySystem.registerObserver(app); 67securitySystem.registerObserver(light); 68securitySystem.setState("Intrusion Detected"); 69 70// Expected Output: 71// SecurityApp notified. New state: Intrusion Detected 72// SecurityLight activated! State: Intrusion Detected
The securitySystem
instance notifies both SecurityApp
and SecurityLight
when the state changes to "Intrusion Detected."
Here is the complete code to implement the Strategy pattern using JavaScript:
JavaScript1// Define the TemperatureControlStrategy interface 2class TemperatureControlStrategy { 3 controlTemperature(temperature) { 4 throw new Error('TemperatureControlStrategy method must be implemented.'); 5 } 6} 7 8// Implement the HeatingStrategy 9class HeatingStrategy extends TemperatureControlStrategy { 10 controlTemperature(temperature) { 11 console.log(`Heating to ${temperature} degrees.`); 12 } 13} 14 15// Implement the CoolingStrategy 16class CoolingStrategy extends TemperatureControlStrategy { 17 controlTemperature(temperature) { 18 console.log(`Cooling to ${temperature} degrees.`); 19 } 20} 21 22// Define the ClimateControl context 23class ClimateControl { 24 constructor(strategy) { 25 this._strategy = strategy; 26 } 27 28 setStrategy(strategy) { 29 this._strategy = strategy; 30 } 31 32 controlTemperature(temperature) { 33 this._strategy.controlTemperature(temperature); 34 } 35} 36 37// Example usage 38const climateControl = new ClimateControl(new HeatingStrategy()); 39climateControl.controlTemperature(22); // Expected Output: Heating to 22 degrees. 40 41climateControl.setStrategy(new CoolingStrategy()); 42climateControl.controlTemperature(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.
By implementing the Observer and Strategy patterns, our smart home system becomes more robust and versatile. The Observer pattern ensures that our security system is responsive by notifying observers of changes, while the Strategy pattern provides flexibility to our climate control system by allowing different temperature control strategies to be used interchangeably. This combination leads to a more adaptable and efficient smart home environment.