파사드 패턴(Facade Pattern)은 복잡한 서브시스템에 단순화된 인터페이스를 제공하는 구조 패턴이다. 외부에서는 파사드만 알면 되고 내부 복잡성은 감춰진다.
구조
클라이언트 → Facade → SubSystem A
→ SubSystem B
→ SubSystem C
Python 구현: 홈시어터 시스템
python
# 복잡한 서브시스템들
class Amplifier:
def on(self): print("앰프 켜기")
def set_volume(self, v): print(f"볼륨: {v}")
def off(self): print("앰프 끄기")
class DVDPlayer:
def on(self): print("DVD 켜기")
def play(self, movie): print(f"재생: {movie}")
def off(self): print("DVD 끄기")
class Projector:
def on(self): print("프로젝터 켜기")
def wide_screen(self): print("와이드 스크린 모드")
def off(self): print("프로젝터 끄기")
class Lights:
def dim(self, level): print(f"조명 {level}%로 낮춤")
# 파사드
class HomeTheaterFacade:
def __init__(self):
self.amp = Amplifier()
self.dvd = DVDPlayer()
self.projector = Projector()
self.lights = Lights()
def watch_movie(self, movie: str):
print("=== 영화 모드 ===")
self.lights.dim(10)
self.projector.on()
self.projector.wide_screen()
self.amp.on()
self.amp.set_volume(20)
self.dvd.on()
self.dvd.play(movie)
def end_movie(self):
print("=== 종료 ===")
self.dvd.off()
self.amp.off()
self.projector.off()
# 클라이언트는 파사드만 사용
theater = HomeTheaterFacade()
theater.watch_movie("인터스텔라")
theater.end_movie()
파사드 vs 어댑터
| 패턴 | 목적 |
|---|
| 파사드 | 복잡성 단순화 (새 인터페이스) |
| 어댑터 | 인터페이스 변환 (호환성) |
관련 개념