利用例子来讲述委托和事件的用法
在我刚毕业的一年内,无论我怎么看委托和事件都始终无法理解它的原理,也不知道用到那里,就没有去理它了。直到最近看了设计模式的观察者模式演变成委托和事件,我才真正的恍然大悟。也明白了用在了什么地方。
在此感谢TreeLee。让我懂了委托和事件的用处。灵感来源:http://www.cnblogs.com/Terrylee/archive/2006/10/23/Observer_Pattern.html
下面利用某一个股票的价格的例子变动来阐述委托和事件。
例子:一家证劵公司开发一个投资APP(如东方财富,同花顺),这个APP用来展示股票的价格的变动(不考虑买卖股票)。现在用某一股票(股票:TLA)的变动通知所有订阅了此股票的变动的投资者。
这个抽象类有2个属性:股票和当前价格,1个方法(用来通知票价已变动),1个事件。
1 public delegate void NotifyDelegate(object obj); 2 public abstract class abstractAPP 3 { 4 //股票名字 5 protected string product { get; set; } 6 7 //股票价格 8 protected int price { get; set; } 9 10 11 public abstractAPP(string product, int price) 12 { 13 this.product = product; 14 this.price = price; 15 } 16 public int Price 17 { 18 set { this.price=value;} 19 get {return price; } 20 } 21 public string Product 22 { 23 set { this.product = value; } 24 get { return product; } 25 } 26 27 //定义一个委托的事件 28 public NotifyDelegate NvfityEvent; 29 30 //用来执行股票价格已经变动的通知 31 public void Update() 32 { 33 Nvfity(); 34 } 35 36 private void Nvfity() 37 { 38 if (NvfityEvent != null) 39 { 40 NvfityEvent(this); 41 } 42 } 43 }
一个继承abstractAPP子类:
1 public class APP:abstractAPP 2 { 3 public APP(string product, int price) 4 : base(product, price) 5 { 6 7 } 8 }
投资者类:
1 public class Investor 2 { 3 private string name; 4 public Investor(string name) 5 { 6 this.name = name; 7 } 8 9 10 public void Receive(object obj) 11 { 12 if (obj is abstractAPP) 13 { 14 var model = (abstractAPP)obj; 15 16 Console.WriteLine("通知:{0} 股票:{1},当前股票价格:{2:C}", name, model.Product, model.Price); 17 18 } 19 } 20 }
移动设备端(也是用来接收通知):
1 public class Mobile 2 { 3 private string no; 4 public Mobile(string no) 5 { 6 this.no = no; 7 } 8 9 public void Receive(object obj) 10 { 11 if (obj is abstractAPP) 12 { 13 var model = (abstractAPP)obj; 14 Console.WriteLine("{0}已接收到通知,产品:{1},价格:{2}", no,model.Product, model.Price); 15 } 16 } 17 }
Client端:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 7 8 //初始值:股票名字:TLA,价格:12 9 abstractAPP app = new APP("TLA", 12); 10 11 //定义一个投资者对象,这个对象名叫mike 12 13 //为了快捷一点就不抽象出父类出来了。 14 Investor inverstor = new Investor("mike"); 15 16 Mobile mobile = new Mobile("vivoXX"); 17 18 19 //订阅了TLA股票的事件 20 app.NvfityEvent += inverstor.Receive; 21 app.NvfityEvent += mobile.Receive; 22 23 24 //发起通知 25 app.Update(); 26 //TLA股票的价格变了 27 app.Price = 13; 28 29 Console.WriteLine(); 30 //通知所有订阅者,股票价格改变了 31 Console.WriteLine("-------------价格变了!---------------------"); 32 33 System.Threading.Thread.Sleep(1000); 34 Console.WriteLine(); 35 Console.WriteLine("-------------发起通知!---------------------"); 36 37 //发起通知(告知所有订阅者) 38 app.Update(); 39 40 41 app.Price = 12; 42 Console.WriteLine(); 43 //再次通知所有订阅者,股票价格改变了 44 Console.WriteLine("-------------价格变了!---------------------"); 45 46 System.Threading.Thread.Sleep(1000); 47 48 Console.WriteLine("-------------发起通知!---------------------"); 49 Console.WriteLine(); 50 //发起通知(告知所有订阅者) 51 app.Update(); 52 53 // Investor投资者已经卖掉了股票不在看此股票了,就不在订阅了 54 app.NvfityEvent -= inverstor.Receive; 55 56 57 //股票价格继续改动 58 Console.WriteLine(); 59 app.Price = 11; 60 //再次通知所有订阅者,股票价格改变了 61 Console.WriteLine("-------------价格变了!---------------------"); 62 63 System.Threading.Thread.Sleep(1000); 64 65 Console.WriteLine("-------------发起通知!---------------------"); 66 //此时只有移动设备端订阅了此事件 67 Console.WriteLine(); 68 app.Update(); 69 70 71 Console.ReadKey(); 72 73 74 75 76 77 } 78 }