利用委托实现观察者模式

  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.Collections.Generic;
  4 None.gif
  5 None.gif public   class  MyClass
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7InBlock.gif    public static void Main()
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        Mouse m = new Mouse();
 10InBlock.gif        People p = new People();
 11InBlock.gif        
 12InBlock.gif        HandleDelegate m_h = new HandleDelegate(m.Run);
 13InBlock.gif        HandleDelegate p_h = new HandleDelegate(p.Wakeup);
 14InBlock.gif            
 15InBlock.gif        Observer ob = new Observer();
 16InBlock.gif        ob.AddObserver(m_h);
 17InBlock.gif        ob.AddObserver(p_h);
 18InBlock.gif        Cat cat = new Cat(ob);
 19InBlock.gif        cat.Cry();
 20InBlock.gif        ob.RemoveObserver(p_h);
 21InBlock.gif        cat.Cry();
 22InBlock.gif        RL();
 23ExpandedSubBlockEnd.gif    }

 24InBlock.gif    
 25ContractedSubBlock.gifExpandedSubBlockStart.gif    Helper methods#region Helper methods
 26InBlock.gif
 27InBlock.gif    private static void WL(object text, params object[] args)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 29InBlock.gif        Console.WriteLine(text.ToString(), args);    
 30ExpandedSubBlockEnd.gif    }

 31InBlock.gif    
 32InBlock.gif    private static void RL()
 33ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 34InBlock.gif        Console.ReadLine();    
 35ExpandedSubBlockEnd.gif    }

 36InBlock.gif    
 37InBlock.gif    private static void Break() 
 38ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 39InBlock.gif        System.Diagnostics.Debugger.Break();
 40ExpandedSubBlockEnd.gif    }

 41InBlock.gif
 42ExpandedSubBlockEnd.gif    #endregion

 43ExpandedBlockEnd.gif}

 44 None.gif
 45 None.gif
 46 None.gif public   delegate   void  HandleDelegate();
 47 None.gif
 48 None.gif public   class  Cat
 49 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 50InBlock.gif    private Observer ob;
 51InBlock.gif    
 52InBlock.gif    public Cat(Observer _ob)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 54InBlock.gif        ob = _ob;
 55ExpandedSubBlockEnd.gif    }

 56InBlock.gif    
 57InBlock.gif    public void Cry()
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 59InBlock.gif        Console.WriteLine("Cat cry dot.gif.");
 60InBlock.gif        ob.Do();
 61ExpandedSubBlockEnd.gif    }

 62ExpandedBlockEnd.gif}

 63 None.gif
 64 None.gif public   class  Mouse
 65 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 66InBlock.gif    public void Run()
 67ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 68InBlock.gif        Console.WriteLine("Runingdot.gif.");
 69ExpandedSubBlockEnd.gif    }

 70ExpandedBlockEnd.gif}

 71 None.gif
 72 None.gif public   class  People
 73 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 74InBlock.gif    public void Wakeup()
 75ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 76InBlock.gif        Console.WriteLine("Wakeupdot.gif");
 77ExpandedSubBlockEnd.gif    }

 78ExpandedBlockEnd.gif}

 79 None.gif
 80 None.gif public   class  Observer
 81 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 82InBlock.gif    private IList<HandleDelegate> list = null;
 83InBlock.gif    
 84InBlock.gif    public Observer()
 85ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 86InBlock.gif        list  = new List<HandleDelegate>();    
 87ExpandedSubBlockEnd.gif    }

 88InBlock.gif    
 89InBlock.gif    public void AddObserver(HandleDelegate h)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 91InBlock.gif        list.Add(h);
 92ExpandedSubBlockEnd.gif    }

 93InBlock.gif    public void RemoveObserver(HandleDelegate h)
 94ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 95InBlock.gif        if(list.Contains(h))
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            list.Remove(h);
 98ExpandedSubBlockEnd.gif        }

 99InBlock.gif        else
100InBlock.gif            return;
101ExpandedSubBlockEnd.gif    }

102InBlock.gif    
103InBlock.gif    public void Do()
104ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
105InBlock.gif        foreach(HandleDelegate h in list)
106ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
107InBlock.gif            h();
108ExpandedSubBlockEnd.gif        }

109ExpandedSubBlockEnd.gif    }

110InBlock.gif    
111ExpandedBlockEnd.gif}

面试的时候遇到这样一个问题,整理下

转载于:https://www.cnblogs.com/flyingchen/archive/2007/03/09/669542.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值