Last active
October 24, 2021 15:42
-
-
Save cch12313/78b4b48926003edab258e1d2b3f338fd to your computer and use it in GitHub Desktop.
Observer Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace design_pattern | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var people1 = new People("Alvin"); | |
var people2 = new People("Lydia"); | |
var people3 = new People("Pekola"); | |
var trafficLight = new TrafficLight(); | |
//訂閱 | |
//按下有聲號誌鈕,但Pekola戴著耳機沒有聽到 | |
trafficLight.RegisterObserver(people1); | |
trafficLight.RegisterObserver(people2); | |
//綠燈 | |
trafficLight.SetLightState(TrafficLightState.Green); | |
// Alvin過馬路囉! | |
// Lydia過馬路囉! | |
//紅燈 | |
trafficLight.SetLightState(TrafficLightState.Red); | |
// Alvin紅燈停! | |
// Lydia紅燈停! | |
//Pekola拿下了耳機 | |
trafficLight.RegisterObserver(people3); | |
trafficLight.SetLightState(TrafficLightState.Green); | |
// Alvin過馬路囉! | |
// Lydia過馬路囉! | |
// Pekola過馬路囉! | |
} | |
public interface ISubject | |
{ | |
public void RegisterObserver(IObserver observer); | |
public void RemoveObserver(IObserver observer); | |
public void NotifyObserver(); | |
} | |
// 觀察者介面 - 讓Subject更新Observer狀態 | |
public interface IObserver | |
{ | |
public void Update(TrafficLightState trafficLightState); | |
} | |
public enum TrafficLightState | |
{ | |
Default = 0, | |
Red = 1, | |
Yellow = 2, | |
Green = 3, | |
} | |
// 紅綠燈實作ISubject | |
public class TrafficLight : ISubject | |
{ | |
//儲存所有訂閱的觀察者 | |
private List<IObserver> _observers; | |
private TrafficLightState _lightState; | |
private void SetLightState() | |
{ | |
NotifyObserver(); | |
} | |
public TrafficLightState GetLightState => _lightState; | |
public void SetLightState(TrafficLightState lightState) | |
{ | |
_lightState = lightState; | |
NotifyObserver(); | |
} | |
public TrafficLight() | |
{ | |
_observers = new List<IObserver>(); | |
} | |
public void RegisterObserver(IObserver observer) | |
{ | |
_observers.Add(observer); | |
} | |
public void RemoveObserver(IObserver observer) | |
{ | |
_observers.Remove(observer); | |
} | |
public void NotifyObserver() | |
{ | |
foreach (var observer in _observers) | |
{ | |
observer.Update(_lightState); | |
} | |
// 也可以這樣寫 | |
// _observers.ForEach(observer => observer.Update(_lightState)); | |
} | |
} | |
public class People : IObserver | |
{ | |
private readonly string _name; | |
public People(string name) | |
{ | |
_name = name; | |
} | |
public void Update(TrafficLightState trafficLightState) | |
{ | |
if (trafficLightState == TrafficLightState.Green) | |
Walk(); | |
if (trafficLightState == TrafficLightState.Red) | |
Stop(); | |
} | |
public void Walk() | |
{ | |
Console.WriteLine($"{_name}過馬路囉!"); | |
} | |
public void Stop() | |
{ | |
Console.WriteLine($"{_name}紅燈停!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment