C#标准事件流

  • A+
所属分类:.NET技术
摘要

  


服装价格变动,触发淘宝发布活动和消费者购买衣服事件流

 1     public class EventStandard  2     {  3         public class Clothes {  4   5             /// <summary>  6             /// 服装编码  7             /// </summary>  8             public string Id { get; set; }  9  10             /// <summary> 11             /// 服装名称 12             /// </summary> 13             public string Name { get; set; } 14  15             /// <summary> 16             /// 服装价格 17             /// </summary> 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             /// <summary> 33             /// 服装价格变动事件 34             /// </summary> 35             public event EventHandler PriceRiseHandler; 36  37         } 38  39         /// <summary> 40         /// 衣服价格事件参数  一般会为特定的事件去封装个参数类型 41         /// </summary> 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             /// <summary> 50             /// 淘宝订户 51             /// </summary> 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             /// <summary> 66             /// 消费者订户 67             /// </summary> 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();

C#标准事件流

clothes.Price = 98;  EventStandard.Show();

C#标准事件流