Facade Design Pattern

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()        

要查看或添加评论,请登录

Mohammad Dillawar的更多文章

  • Git Installation and Setup Tutorial

    Git Installation and Setup Tutorial

    1. Introduction to Git Git is a distributed version control system that helps track changes in source code during…

  • Django-Tenants A Professional Developer's Guide

    Django-Tenants A Professional Developer's Guide

    Introduction Django, the high-level Python web framework, is widely appreciated for its simplicity and…

    4 条评论
  • Understanding the class Keyword in JavaScript

    Understanding the class Keyword in JavaScript

    JavaScript, originally designed as a scripting language for web browsers, has evolved into a versatile and powerful…

  • Exploring JavaScript Function Types

    Exploring JavaScript Function Types

    Certainly! Let's delve into the details of JavaScript functions, covering various aspects such as function declaration,…

社区洞察

其他会员也浏览了