第506篇-Delegate和Event异同--(内容篇2:共6篇)

本文用Mouse,Cat,Host的例子,讲解设计模式中非常著名的观察者模式:Cat叫的时候, Mouse开始Escape, Host惊醒。

这是观察者和动作发起者:

 public delegate void ShoutDelegate(string str);
    public class Cat
    {
        public event ShoutDelegate ShoutEvent;
        public void OnShout(string str)
        {
            if (ShoutEvent!=null)
            {
                ShoutEvent(str);
            }
        }
    }

    public class ObserveeBase
    {
        public string Name { get; set; }
    }

    public class Mouse : ObserveeBase 
    {
        public Mouse(string name, Cat cat)
        {
            this.Name = name;
            cat.ShoutEvent += new ShoutDelegate(OnAction);
        }

        void OnAction(string str)
        {
            Console.WriteLine(string.Format("{0},mouse {1} is escaping...",str,Name)); 
        }
    }

    public class Host : ObserveeBase
    {
        public string Name { get; set; }
        public Host(string name, Cat cat)
        {
            this.Name = name;
            cat.ShoutEvent += new ShoutDelegate(OnAction);
        }

        void OnAction(string str)
        {
            Console.WriteLine(string.Format("{0}, Host {1} is waking...",str, Name));
        }
    }

这是客户端代码:

 static void Main1(string[] args)
        {
            Cat cat = new Cat();
            Mouse mouse = new Mouse("mouse1", cat);
            Host host = new Host("host1", cat);

            // Cat is shouting...
            cat.OnShout("Cat is shouting..");

            ShoutDelegate dele = new ShoutDelegate(Method1);
            dele += Method2;
            dele("test");
        }

从上面例子中,我们可以看出,每一次实例化Observee的时候,到告诉了当前这个Observee,你要观察的是哪一个对象(比如Cat),然后在Observee类里面实现自己的逻辑。

运行结果:

 

#2:当然,我们也可以不在Observee类里面订阅事件,在Client中订阅事件(不推荐这样做),如下:

  public class Mouse : ObserveeBase 
    {
        //public Mouse(string name, Cat cat)
        //{
        //    this.Name = name;
        //    cat.ShoutEvent += new ShoutDelegate(OnAction);
        //}
        public Mouse(string name)
        {
            this.Name = name;
        }
       public void OnAction(string str)
        {
            Console.WriteLine(string.Format("{0},mouse {1} is escaping...",str,Name)); 
        }
    }

    public class Host : ObserveeBase
    {
        public string Name { get; set; }
        //public Host(string name, Cat cat)
        //{
        //    this.Name = name;
        //    cat.ShoutEvent += new ShoutDelegate(OnAction);
        //}
        public Host(string name)
        {
            this.Name = name;
        }

       public  void OnAction(string str)
        {
            Console.WriteLine(string.Format("{0}, Host {1} is waking...",str, Name));
        }
    }

// Client:
 Cat cat = new Cat();
            Mouse mouse = new Mouse("mouse1");
            Host host = new Host("host1");
            cat.ShoutEvent += new ShoutDelegate(mouse.OnAction);
            cat.ShoutEvent += new ShoutDelegate(host.OnAction);

            // Cat is shouting...
            cat.OnShout("Cat is shouting..");

运行结果是一样的.
有趣的是,如果我们把Event关键字去掉,结果是一样的:

public delegate void ShoutDelegate(string str);
    public class Cat
    {
        public  ShoutDelegate ShoutEvent;
        //public event ShoutDelegate ShoutEvent;
        public void OnShout(string str)
        {
            if (ShoutEvent!=null)
            {
                ShoutEvent(str);
            }
        }
    }

但是Event可以:你不想在声明delegate的类之外调用delegate,还有,除了声明Event的类,其它类只能订阅(Subscribe,即+=)或取消订阅(Unsubscribe),这样就更封装性了.

posted @ 2013-03-04 17:34  Shanghai Jim Zhou  阅读(208)  评论(0编辑  收藏  举报