Delegate&&Event

引入:(观察者模式)

准备工作:

1 public interface IObject
2     {
3         void DoAction();
4     }
interface IObject
 1 public class Baby : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Cry();
 6         }
 7 
 8         public void Cry()
 9         {
10             Console.WriteLine("{0} Cry", this.GetType().Name);
11         }
12     }
Baby : IObject
 1 public class Brother : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Turn();
 6         }
 7         public void Turn()
 8         {
 9             Console.WriteLine("{0} Turn", this.GetType().Name);
10         }
11     }
Brother : IObject
 1 public class Dog : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Wang();
 6         }
 7         public void Wang()
 8         {
 9             Console.WriteLine("{0} Wang", this.GetType().Name);
10         }
11     }
Dog : IObject
 1 public class Father : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Roar();
 6         }
 7         public void Roar()
 8         {
 9             Console.WriteLine("{0} Roar", this.GetType().Name);
10         }
11     }
Father : IObject
 1 public class Mother : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Wispher();
 6         }
 7         public void Wispher()
 8         {
 9             Console.WriteLine("{0} Wispher", this.GetType().Name);
10         }
11     }
Mother : IObject
 1 public class Mouse : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Run();
 6         }
 7         public void Run()
 8         {
 9             Console.WriteLine("{0} Run", this.GetType().Name);
10         }
11     }
Mouse : IObject
 1 public class Neighbor : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Awake();
 6         }
 7         public void Awake()
 8         {
 9             Console.WriteLine("{0} Awake", this.GetType().Name);
10         }
11     }
Neighbor : IObject
 1 public class Stealer : IObject
 2     {
 3         public void DoAction()
 4         {
 5             this.Hide();
 6         }
 7         public void Hide()
 8         {
 9             Console.WriteLine("{0} Hide", this.GetType().Name);
10         }
11     }
Stealer : IObject

当毛叫一声,会触发其他事件的发生。

 1 public class Cat
 2     {
 3         public void Miao()
 4         {
 5             Console.WriteLine("{0} Miao", this.GetType().Name);
 6 
 7 
 8             new Mouse().Run();
 9             new Dog().Wang();
10             new Baby().Cry();
11             new Brother().Turn();
12             new Mother().Wispher();
13             new Father().Roar();
14             new Neighbor().Awake();
15             new Stealer().Hide();
16 
17         }
18     }
Miao()
1                     Cat cat = new Cat();
2                     cat.Miao();//猫叫,且触发其它事件一起发生。

过渡(由委托到事件):

单独的类Cat.cs里

1         public Action MiaoAction;
2         public void MiaoActionMethod()
3         {
4             Console.WriteLine("{0} MiaoActionMethod", this.GetType().Name);
5             if (this.MiaoAction != null)
6                 this.MiaoAction.Invoke();
7         }

调用(在program.cs文件中)

1                     cat.MiaoAction += new Mouse().Run;
2                     cat.MiaoAction += new Dog().Wang;
3                     cat.MiaoAction += new Baby().Cry;
4                     cat.MiaoAction += new Brother().Turn;
5                     cat.MiaoAction += new Mother().Wispher;
1                     cat.MiaoAction += new Father().Roar;
2                     cat.MiaoAction += new Neighbor().Awake;
3                     cat.MiaoAction += new Stealer().Hide;
4                     cat.MiaoActionMethod();//通过方法间接调用委托

由委托做到了一样的工作,且添加事件更方便。

1                     cat.MiaoAction.Invoke();//也可以直接调用委托

用Event事件来做:

Cat.cs类里:

 1         /// <summary>
 2         /// 一组方法
 3         /// 事件:委托的实例,加上一个event关键字
 4         /// 委托是一种类型(Student),事件是委托的一个实例(青青-人名)
 5         /// event关键字限制了权限,保证安全
 6         /// </summary>
 7         public event Action MiaoEvent;
 8         public void MiaoEventMethod()
 9         {
10             Console.WriteLine("{0} MiaoActionMethod", this.GetType().Name);
11             if (this.MiaoEvent != null)
12                 this.MiaoEvent.Invoke();//在类内部调用
13 
14             //this.MiaoEvent = null;
15 
16         }

Program.cs类里:

1                     cat.MiaoEvent += new Mouse().Run;
2                     cat.MiaoEvent += new Dog().Wang;
3                     cat.MiaoEvent += new Baby().Cry;
4                     cat.MiaoEvent += new Brother().Turn;
5                     cat.MiaoEvent += new Mother().Wispher;
6                     cat.MiaoEvent += new Father().Roar;
7                     cat.MiaoEvent += new Neighbor().Awake;
8                     cat.MiaoEvent += new Stealer().Hide;
9                     cat.MiaoEventMethod();//只能通过方法间接调用,外部只能订阅方法,不能调用Event.

                      cat.MiaoEvent.Invoke();//在外部调用会报错,不能直接调用。

 (外部订阅,内部发布---触发。)

进一步理解,在Winform中,点击按钮就可以生成事件就是基于这种机制。

我们负者把执行过程设计出来,并把这个过程交给系统,叫订阅。

Form1.cs: (双击进入)

        /// <summary>
        /// 按钮动作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, EventArgs e)
        {
            Console.WriteLine("btnTest_Click");
        }
        

Form1.Designer.cs:

            // 
            // btnTest
            // 
            this.btnTest.Location = new System.Drawing.Point(58, 44);
            this.btnTest.Name = "btnTest";
            this.btnTest.Size = new System.Drawing.Size(123, 72);
            this.btnTest.TabIndex = 0;
            this.btnTest.Text = "Test";
            this.btnTest.UseVisualStyleBackColor = true;
            this.btnTest.Click += new System.EventHandler(this.btnTest_Click);//订阅

当满处条件时就会触发该事件的执行,叫触发(当点击Test按钮时)。

原文地址:https://www.cnblogs.com/anwser-jungle/p/8413317.html