Observer模式和委托事件

事件委托:event-delegate

事件是特殊的委托,无论我们是否定义public的事件,最终都会被编译成private的委托。

所以事件在外部无法赋值,但是事件有公共的remove,add方法,我们可以通过+=来注册事件。

委托必须先=赋值,然后才能用+=来附加委托。

通过IL DASM查看编译结果:

 

___[MOD] C:/MouseCatExample.exe
   |      M A N I F E S T
   |___[NSP] MouseCatExample
   |   |___[CLS] MouseCatExample.Cat
   |   |   |     .class public auto ansi beforefieldinit 
   |   |   |___[FLD] Eventhandler : private class MouseCatExample.Delegate
   |   |   |___[MET] .ctor : void()
   |   |   |___[MET] FireAway : void()
   |   |   |___[MET] add_Eventhandler : void(class MouseCatExample.Delegate)
   |   |   |___[MET] remove_Eventhandler : void(class MouseCatExample.Delegate)
   |   |   |___[EVT] Eventhandler : MouseCatExample.Delegate
   |   |
   |   |___[CLS] MouseCatExample.Delegate
   |   |   |     .class public auto ansi sealed 
   |   |   |      extends [mscorlib]System.MulticastDelegate 
   |   |   |___[MET] .ctor : void(object,native int)
   |   |   |___[MET] BeginInvoke : class [mscorlib]System.IAsyncResult(class [mscorlib]System.AsyncCallback,object)
   |   |   |___[MET] EndInvoke : void(class [mscorlib]System.IAsyncResult)
   |   |   |___[MET] Invoke : void()
   |   |
   |   |___[CLS] MouseCatExample.Master
   |   |   |     .class public auto ansi beforefieldinit 
   |   |   |___[MET] .ctor : void(class MouseCatExample.Cat)
   |   |   |___[MET] Action : void()
   |   |
   |   |___[CLS] MouseCatExample.Mouse
   |   |   |     .class public auto ansi beforefieldinit 
   |   |   |___[MET] .ctor : void(class MouseCatExample.Cat)
   |   |   |___[MET] Action : void()
   |   |
   |   |___[CLS] MouseCatExample.Program
   |   |   |     .class private auto ansi beforefieldinit 
   |   |   |___[MET] .ctor : void()
   |   |   |___[STM] Main : void(string[])
   |   |
   |

Case1:CatMouse:

public delegate void Delegate();

    public class Cat
    {
        public event Delegate Eventhandler;

        public void FireAway()
        {
            if (this.Eventhandler != null)
            {
                this.Eventhandler();
            }
        }
    }
    public class Master
    {
        public Master(Cat cat)
        {
            cat.Eventhandler += new Delegate(Action);
        }
        public void Action()
        {
            Console.WriteLine("master heared!");
        }
    }
    public class Mouse
    {
        public Mouse(Cat cat)
        {
            cat.Eventhandler += new Delegate(Action);
        }
        public void Action()
        {
            Console.WriteLine("mouse heared and run!");
        }
    }

static void Main(string[] args)
        {
            Cat cat = new Cat();
            Mouse mouse = new Mouse(cat);
            Master master = new Master(cat);
            cat.FireAway();
            Console.Read();

        }
View Code

Case2: HeaterAlarm

using System;
 using System.Collections.Generic;
 using System.Text;
 namespace Delegate {
        // 热水器
       public class Heater {
               private int temperature;
               public delegate void BoilHandler(int param);       //声明委托
              public event BoilHandler BoilEvent;                     //声明事件 

              // 烧水
              public void BoilWater() {
                      for (int i = 0; i <= 100; i++) {
                             temperature = i;

Www~
                             if (temperature > 95) {
                                    if (BoilEvent != null) {       //如果有对象注册
                                          BoilEvent(temperature);       //调用所有注册对象的方法
                                   } 
                             }
                      }
               }
        }
        // 警报器
       public class Alarm {
               public void MakeAlert(int param) {
                      Console.WriteLine(”Alarm:嘀嘀嘀,水已经 {0} 度了:”, param);
               }
        }

       // 显示器
       public class Display {
               public static void ShowMsg(int param) {       //静态方法
                     Console.WriteLine(”Display:水快烧开了,当前温度:{0}度。”, param);
               }
        }
       
        class Program {
               static void Main() {
                      Heater heater = new Heater();
                      Alarm alarm = new Alarm();

.

                     heater.BoilEvent += alarm.MakeAlert;       //注册方法
                     heater.BoilEvent += (new Alarm()).MakeAlert;       //给匿名对象注册方法
                     heater.BoilEvent += Display.ShowMsg;              //注册静态方法 .

                     heater.BoilWater();       //烧水,会自动调用注册过对象的方法
              }
        }
 }
View Code
原文地址:https://www.cnblogs.com/lemonP/p/7059895.html