设计模式

设计模式 - 观察者模式

2016-05-01  本文已影响37人  Mitchell

一、观察者模式概述

+观察者模式又叫做发布-订阅模式
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同事监听某一个主题对象,使它们能够自动更新自己。


二、什么时候使用观察者模式


三、委托

       //通知者接口     
       interface Subject
       {
           void Attatch(Observer observer);
           void Detach(Observer observer);
           void Notify();
           string SubjectState
           {
               get;
               set;
           }
       }       
       //声明一个委托
       delegate void EventHandler();
       class Boss: Subject
       {
           //声明一个事件 Update,类型为委托 EventHandler
           public event EventHandler Update;
           
           //同事列表
        //    private IList<Observer> observers = new List<observer>();
           private string action;
        //    //add
        //    public void Attatch(Observer observer)
        //    {
        //        observers.Add(observer);
        //    }
           
        //    //minus
        //    public void Detach(Observer observer)
        //    {
        //        observers.Remove(observer);
        //    }
           //notify
           public void Notify()
           {
                Update();
           }
           //Boss state
           public string SubjectState
           {
               get{return action;}
               set{action = value;}
           }
       }
       class Secretary:Subject
       {
                      //同事列表
           private IList<Observer> observers = new List<observer>();
           private string action;
           //add
           public void Attatch(Observer observer)
           {
               observers.Add(observer);
           }
           
           //minus
           public void Detach(Observer observer)
           {
               observers.Remove(observer);
           }
           
           //notify
           public void Notify()
           {
               foreach (Observer o in observers)
               {
                   o.Update();
               }
           }
           //Boss state
           public string SubjectState
           {
               get{return action;}
               set{action = value;}
           }   
       }
       //abstract observer
       abstract class Observer
       {
           protected string name;
           protected Subject sub;
           
           public Observer(string name,Subject sub)
           {
               this.name = name;
               this.sub = sub;
               
               public abstract void Update();
           }
           
       }
       class StockObserver
       {
           public StockObserver(string name.Subject sub):base(name,sub)
           {
            
           }
           public override void CloseStockMarket()
           {
               Console.WriteLine("{0} {1} 关闭股票行情,继续工作!",sub.SubjectState,name);
           }
       }
       class NBAObserver
       {
           private string name;
           private Subject sub;
           public NBAObserver(string name,Subject sub)
           {
               this.name = name;
               this.sub = sub;
           }
           //close NBA
           public void CloseNBADirectSeeding()
           {
               Console.WriteLine("{0} {1} 关闭NBA,继续工作!",sub.SubjectState,name);
           }
       }
       //客户端主函数
       class MainClass
       {
           public static void Main(strnig[] args)
           {
               //老板
               Boss boss = new Boss();
               //同事1
               StockObserver one = new StockObserver("nameOne",boss);
               //同事2
               StockObserver two = new StockObserver("nameTwo",boss);
               
               boss.Update += EventHandler(one.CloseStockMarket);
               boss.Update += EventHandler(two.CloseNBADirectSeeding);               
            //    boss.Attatch(one);
            //    boss.Attatch(two);
            //    boss.Detach(one);
               boss.SubjectState = "Come Back";
               boss.Notify(); 
          }
       }
       ```
上一篇下一篇

猜你喜欢

热点阅读