C#标准事件流
标签:tar show name handler tao 特定 seh 活动 string
服装价格变动,触发淘宝发布活动和消费者购买衣服事件流
1 public class EventStandard
2 {
3 public class Clothes {
4
5 ///
6 /// 服装编码
7 ///
8 public string Id { get; set; }
9
10 ///
11 /// 服装名称
12 ///
13 public string Name { get; set; }
14
15 ///
16 /// 服装价格
17 ///
18 private double _price;
19
20 public double Price {
21 get { return this._price; }
22 set {
23 PriceRiseHandler?.Invoke(this, new PriceEventArgs()
24 {
25 OldPrice = this._price,
26 NewPrice = value
27 });
28 this._price = value;
29 }
30 }
31
32 ///
33 /// 服装价格变动事件
34 ///
35 public event EventHandler PriceRiseHandler;
36
37 }
38
39 ///
40 /// 衣服价格事件参数 一般会为特定的事件去封装个参数类型
41 ///
42 public class PriceEventArgs : EventArgs
43 {
44 public double OldPrice { get; set; }
45 public double NewPrice { get; set; }
46 }
47
48 public class TaoBao {
49 ///
50 /// 淘宝订户
51 ///
52 public void PublishPriceInfo(object sender, EventArgs e) {
53 Clothes clothes = (Clothes)sender;
54 PriceEventArgs args = (PriceEventArgs)e;
55 if (args.NewPrice args.OldPrice)
56 Console.WriteLine($"淘宝:发布衣服价格下降的公告,{clothes.Name}服装直降{args.OldPrice - args.NewPrice}元,限时抢购!");
57 else
58 Console.WriteLine("淘宝:价格悄悄上涨或价格未变化,啥也不做");
59 }
60
61 }
62
63 public class Consumer
64 {
65 ///
66 /// 消费者订户
67 ///
68 public void Buy(object sender, EventArgs e)
69 {
70 Clothes clothes = (Clothes)sender;
71 PriceEventArgs args = (PriceEventArgs)e;
72 if (args.NewPrice args.OldPrice)
73 Console.WriteLine($"消费者:之前价格{args.OldPrice},现在价格{args.NewPrice},果断买了!");
74 else
75 Console.WriteLine($"消费者:等等看,降价了再说");
76 }
77 }
78
79 public static void Show()
80 {
81 Clothes clothes = new Clothes()
82 {
83 Id = "12111-XK",
84 Name = "优衣库",
85 Price = 128
86 };
87 //订阅:把订户和发布者的事件关联起来
88 clothes.PriceRiseHandler += new TaoBao().PublishPriceInfo;
89 clothes.PriceRiseHandler += new Consumer().Buy;
90 //价格变化,自动触发订户订阅的事件
91 clothes.Price = 300;
92 }
93
94 }
调用:
clothes.Price = 300;
EventStandard.Show();
clothes.Price = 98;
EventStandard.Show();
C#标准事件流
标签:tar show name handler tao 特定 seh 活动 string
原文地址:https://www.cnblogs.com/netlws/p/13364010.html
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
C#标准事件流
文章链接:http://soscw.com/index.php/essay/38434.html
评论