2020年7月23日星期四

C#标准事件流

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

 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 = value27       });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 : EventArgs43   {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 Consumer64   {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 = 12886    };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#标准事件流skyeejpgoodbuydamai亚马逊对快递标签提出要求, 规范贴标行为!如何组建一支高效的亚马逊刷单测评团队?321跨境电商早报0412丨亚马逊日本站Prime会员会费首次上调云南双廊:上帝遗落的美梦(组图)光棍节彻底"脱光" 国内外艳遇地推荐(组图)_丽江市旅游夜登泰山行程小记_泰安攻略

没有评论:

发表评论