事件 代理 练习

  十分不喜欢 委托那个词.

cat.cs: 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace delegateTry
 7 {
 8     public delegate void CatShoutEventHandler(object sender, ShoutEventArgs e);
 9 
10     class Cat
11     {
12         public event CatShoutEventHandler CatShoutEvent;
13 
14         private string name;
15 
16         public Cat(string name)
17         {
18             this.name = name;
19         }
20 
21 
22         public string Name
23         {
24             get
25             {
26                 return name;
27             }
28         }
29 
30         public void Shout()
31         {
32             if (this.CatShoutEvent != null)
33             {
34                 this.CatShoutEvent(thisnew ShoutEventArgs(this.name));
35             }
36         }
37 
38     }
39 }

40  

Mouse.cs:

  1 using System;

 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace delegateTry
 7 {
 8     class Mouse
 9     {
10         private string name;
11 
12         public Mouse(string name)
13         {
14             this.name = name;
15         }
16 
17         public void run(object sender,ShoutEventArgs e)
18         {
19             Console.WriteLine(" {0}  来了, 我 {1} 开溜! ",e.ShoutSource,this.name);
20         }
21 
22     }
23 
24 }
25 

shoutEventArgs.cs:

  1 using System;

 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace delegateTry
 7 {
 8   public class ShoutEventArgs : EventArgs
 9     {
10         private string shoutSource;
11 
12         public ShoutEventArgs(string shoutSource)
13         {
14             this.shoutSource = shoutSource;
15         }
16 
17         public string ShoutSource
18         {
19             get
20             {
21                 return shoutSource;
22             }
23         }
24   }
25 }
26 

 main:Program.cs

: 1 using System;

 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace delegateTry
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Cat tomcat = new Cat("Tomcat");
13 
14             Mouse jacky = new Mouse("Tacky");
15             Mouse mikky = new Mouse("Mikky");
16 
17 
18             /*下面这段代码 存在一点问题
19              *
20              *  重复 
21              *  
22              * 多个对象如何解决呢 ?
23              * 
24              * 使用 观察着 模式 
25              *
26              * 使得 Observable 单例 来保存 所有实例化 的 Mouse 对象
27              * 
28              * 利用 工厂模式 来管理 生成 Mouse 及其 可能 出现的子类的  对象 
29              * 
30              * 问题 : 事件 通知 如何 到达 所有 的 Observe 对象?  
31              * 
32              * 在 工厂 中 产生 实例 之后 利用 
33              * 
34              * (传进来的Cat)tomcat.CatShoutEvent += new CatShoutEventHandler(this.run); 
35              * 
36              * 上面的问题 解决 之后  真实的 滥用 设计模式了
37              * 
38              * 我也应该 快 疯掉了
39              * 
40              *
41              * */
42             tomcat.CatShoutEvent += new CatShoutEventHandler(jacky.run);
43             tomcat.CatShoutEvent += new CatShoutEventHandler(mikky.run);
44 
45             tomcat.Shout();
46 
47             Console.Read();
48 
49         }
50     }
51 }

52  

写 代码 可以让我的 心 拥有短暂的宁静../ 感觉好些了. 祝看到这里的 朋友  ,生活 中 能够 做真正的自己, 充实的过每一天,快乐的做自己想做的事情.!!      很晚了... 洗洗睡吧..      计划都乱了..╮(╯▽╰)╭..

原文地址:https://www.cnblogs.com/ToDoToTry/p/1510598.html