首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。