Python Facade
The Facade Pattern is a structural design pattern that provides a simplified interface to a complex subsystem. Imagine going to a restaurant: you don't need to know all the details of how the chef chops vegetables, stir-fries, and plates the food in the kitchen; you just need to tell the waiter what you want to eat, and the waiter will coordinate the kitchen to complete all the work. This waiter plays the role of the "Facade".
In software development, the Facade Pattern hides the complexity of a subsystem by creating a unified interface, making client code easier to use.
* * *
## Core Concepts of the Facade Pattern
### Simplifying Complex Systems
As systems become increasingly complex, dependencies between classes also become intricate. The Facade Pattern reduces the coupling between the system and the client by providing a high-level interface.
### Providing a Unified Entry Point
The Facade class acts as the sole entry point to the subsystem. The client only needs to interact with the Facade class without needing to understand the complex internal structure of the subsystem.
### Lowering the Barrier to Entry
For developers unfamiliar with the internal structure of the system, the Facade Pattern allows them to quickly get started using the system's features.
* * *
## Structure of the Facade Pattern
!(#)
From the diagram, we can see:
* **Client**: The code that uses the Facade class
* **Facade**: Provides a simplified interface and coordinates various subsystems
* **Subsystem**: Classes that implement specific functionalities
* * *
## Practical Application Example: Home Theater System
Let's understand the practical application of the Facade Pattern through a home theater example.
### Subsystem Classes
First, we create the various device classes in the home theater:
## Instance
class Amplifier:
"""Amplifier class"""
def on(self):
print("Amplifier turned on")
def set_volume(self, level):
print(f"Volume set to {level}")
def off(self):
print("Amplifier turned off")
class DVDPlayer:
"""DVD Player class"""
def on(self):
print("DVD player turned on")
def play(self, movie):
print(f"Starting to play movie: {movie}")
def stop(self):
print("DVD player stopped")
def off(self):
print("DVD player turned off")
class Projector:
"""Projector class"""
def on(self):
print("Projector turned on")
def set_input(self, source):
print(f"Projector input source set to: {source}")
def off(self):
print("Projector turned off")
class Lights:
"""Lights class"""
def dim(self, level):
print(f"Lights dimmed to {level}%")
def on(self):
print("Lights fully on")
class Screen:
"""Screen class"""
def down(self):
print("Screen lowered")
def up(self):
print("Screen raised")
### Facade Class
Now create the Facade class, which encapsulates all the complex operations of the devices:
## Instance
class HomeTheaterFacade:
"""Home Theater Facade class"""
def __init__ (self):
self.amplifier= Amplifier()
self.dvd_player= DVDPlayer()
self.projector= Projector()
self.lights= Lights()
self.screen= Screen()
def watch_movie(self, movie):
"""Watch movie - one-click completion of all preparation work"""
print("n=== Starting Home Theater Mode ===")
# Start devices in correct order
self.lights.dim(10)# Dim lights
self.screen.down()# Lower screen
YouTip