옵저버 패턴
Coddy Python 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 64개 중 47번째.
옵서버 패턴은 한 객체(주제)의 상태가 변경될 때 여러 객체(옵서버)에게 알림을 보내는 일대다 관계를 생성합니다.
다음은 옵저버를 관리하는 간단한 Subject 클래스입니다:
class Subject:
def __init__(self):
self._observers = []
def add_observer(self, observer):
self._observers.append(observer)
def notify(self, message):
for observer in self._observers:
observer.update(message)주제는 옵저버 목록을 유지하며 모든 옵저버에게 한 번에 알릴 수 있습니다.
간단한 옵서버 클래스를 생성합니다:
class EmailNotifier:
def update(self, message):
print(f"Email sent: {message}")
class SMSNotifier:
def update(self, message):
print(f"SMS sent: {message}")각 옵저버에는 알림을 받을 때 호출되는 update 메서드가 있습니다.
옵서버 패턴을 사용하세요:
# 주제 생성
news = Subject()
# 옵저버 생성
email = EmailNotifier()
sms = SMSNotifier()
# 주제에 옵저버 추가
news.add_observer(email)
news.add_observer(sms)
# 모든 옵저버에게 알림
news.notify("Breaking news: Python is awesome!")주가 추적기를 사용한 실용적인 예제를 만들어 보세요:
class Stock:
def __init__(self, symbol, price):
self.symbol = symbol
self._price = price
self._observers = []
def add_observer(self, observer):
self._observers.append(observer)
def set_price(self, price):
self._price = price
self.notify()
def notify(self):
for observer in self._observers:
observer.update(self.symbol, self._price)
class Investor:
def __init__(self, name):
self.name = name
def update(self, symbol, price):
print(f"{self.name} notified: {symbol} is now ${price}")
# 주식 추적기 사용
apple_stock = Stock("AAPL", 150)
investor1 = Investor("Alice")
investor2 = Investor("Bob")
apple_stock.add_observer(investor1)
apple_stock.add_observer(investor2)
apple_stock.set_price(155) # 모든 투자자에게 알림출력:
Email sent: Breaking news: Python is awesome!
SMS sent: Breaking news: Python is awesome!
Alice notified: AAPL is now $155
Bob notified: AAPL is now $155핵심 사항: 옵서버 패턴을 사용하면 어떤 객체에서 변경이 발생했을 때 한 객체가 여러 다른 객체에 자동으로 알릴 수 있습니다. 주제 객체는 옵서버 목록을 유지하고 필요할 때 해당 옵서버의 update 메서드를 호출합니다. 이는 알림, 이벤트 시스템, 애플리케이션의 여러 부분을 동기화된 상태로 유지하는 데 유용합니다.
챌린지
중급지정된 코드 영역에 두 클래스를 생성하여 Observer Pattern을 구현하고 날씨 모니터링 시스템을 만드세요.
1단계: WeatherStation 클래스 생성
주석이 표시된 곳에 WeatherStation 클래스를 작성하세요. 이 클래스는 다음을 수행해야 합니다.
- Subject 클래스 상속 (
class WeatherStation(Subject):사용) - 다음과 같이 초기화:
super().__init__()을 사용하여 부모 생성자를 호출- 단일 밑줄 속성
self._temperature을 사용하여 초기 온도를 0으로 설정 (Python에서 내부 속성에 사용하는 규칙)
<strong>set_temperature(self, temperature)</strong>구현:- 내부 온도 속성을 업데이트
- 모든 옵저버에게 알리기 위해
self.notify(self._temperature)호출
<strong>get_temperature(self)</strong>구현:- 현재 온도 값을 반환
2단계: WeatherDisplay 클래스 생성
주석이 표시된 곳에 WeatherDisplay 클래스를 작성하세요. 이 클래스는 다음을 수행해야 합니다.
- Observer 클래스 상속 (
class WeatherDisplay(Observer):사용) - 다음과 같이 초기화:
name매개변수를 받음- 이름을
self.name으로 저장
<strong>update(self, temperature)</strong>구현:- 아래에 표시된 정확한 형식으로 온도 업데이트 메시지를 출력
메시지 형식:
디스플레이가 온도 업데이트를 받으면 다음과 정확히 같이 출력해야 합니다.
Display [name]: Current temperature is [temperature]C사용 예시:
# Create weather station and displays
station = WeatherStation()
phone_display = WeatherDisplay("Phone")
tablet_display = WeatherDisplay("Tablet")
# Attach displays to station
station.attach(phone_display)
station.attach(tablet_display)
# Update temperature - both displays will be notified
station.set_temperature(25.5)
# Output:
# Display Phone: Current temperature is 25.5C
# Display Tablet: Current temperature is 25.5C중요 참고 사항:
attach()및detach()메서드는 Subject 기본 클래스에 이미 구현되어 있으므로, WeatherStation 클래스에서 이를 구현하거나 호출할 필요가 없습니다.- 위 사용 예시에서
station.attach(phone_display)는 WeatherStation 구현 내부가 아니라 클래스를 사용하는 사용자가 호출합니다. - WeatherStation은 온도가 변경될 때
self.notify()를 호출하기만 하면 됩니다. 나머지는 Subject 기본 클래스가 처리합니다. - 위에 지정된 대로 WeatherStation에 필요한 세 개의 메서드와 WeatherDisplay에 필요한 두 개의 메서드를 구현하는 데 집중하세요.
직접 해보기
class Subject:
def __init__(self):
self._observers = []
def attach(self, observer):
self._observers.append(observer)
def detach(self, observer):
self._observers.remove(observer)
def notify(self, data):
for observer in self._observers:
observer.update(data)
class Observer:
def update(self, data):
pass
# 여기에 WeatherStation 클래스를 작성하세요
# 여기에 WeatherDisplay 클래스를 작성하세요
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러