Facade Design Pattern
Mohammad Dillawar
Backend Developer | API Specialist | Payment Gateway Proficient | Cloud & DevOps Enthusiast
The Facade design pattern is a structural pattern that provides a simplified and unified interface to a set of interfaces in a subsystem. It encapsulates the complexities of interacting with multiple components within a system by offering a higher-level interface, making it easier for clients to use.
class MotionSensor(object):
def __init__(self):
...
def detect_motion(self):
print("Motion detected")
def stop_detection(self):
print("Motion detection stopped")
class SecurityCamera(object):
def __init__(self):
...
def start_recording(self):
print("Recording started")
def stop_recording(self):
print("Recording stopped")
class AlarmSystem(object):
def __init__(self):
pass
def activate_alarm(self):
print("Alarm activated")
def deactivate_alarm(self):
print("Alarm deactivated")
class HomeSecurityFacade(object):
""" Home Security Facade Design Pattern """
def __init__(self) -> None:
self._motion_sensor = MotionSensor()
self._security_camera = SecurityCamera()
self._alarm_system = AlarmSystem()
def detect_intruder(self):
self._motion_sensor.detect_motion()
self._security_camera.start_recording()
self._alarm_system.activate_alarm()
def no_intruder(self):
self._motion_sensor.stop_detection()
self._security_camera.stop_recording()
self._alarm_system.deactivate_alarm()
if __name__ == "__main__":
home_security_facade = HomeSecurityFacade()
intruder_detected = True
if intruder_detected:
home_security_facade.detect_intruder()
else:
home_security_facade.no_intruder()