码迷,mamicode.com
首页 > 其他好文 > 详细

DesignPattern_Behavioral_Observer

时间:2014-11-17 10:26:16      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   sp   for   div   

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

标签:des   style   blog   io   color   ar   sp   for   div   

原文地址:http://www.cnblogs.com/rammderek/p/4102939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!