首页 > 代码库 > DesignPattern_Behavioral_Observer

DesignPattern_Behavioral_Observer

void Main(){    Subject s = new Subject();    s.Add(new ObserverA(s));    s.Add(new ObserverB(s));    s.Notify();}class Subject{    List<Observer> observers = new List<Observer>();    public void Add(Observer o){ observers.Add(o);}    public void Remove(Observer o){ observers.Remove(o);}    public void Notify(){        foreach (var observer in observers)        {            observer.Show();        }    }}abstract class Observer{    protected Subject subject;    public Observer(Subject s){ subject = s; }    public abstract void Show();}class ObserverA:Observer{    public ObserverA(Subject s):base(s){}    public override void Show(){}}class ObserverB:Observer{    public ObserverB(Subject s):base(s){}    public override void Show(){}}

 

DesignPattern_Behavioral_Observer