C # standard event flow

Time:2021-1-27

Changes in clothing prices trigger Taobao’s publishing activities and the event flow of consumers buying clothes

public class EventStandard
     {
         public class Clothes {
 
             /// 
             ///Clothing code
             /// 
             public string Id { get; set; }
 
             /// 
             ///Clothing name
             /// 
             public string Name { get; set; }
 
             /// 
             ///Clothing price
             /// 
             private double _price;
 
             public double Price {
                 get { return this._price; }
                 set {
                         PriceRiseHandler?.Invoke(this, new PriceEventArgs()
                         {
                             OldPrice = this._price,
                             NewPrice = value
                         });
                     this._price = value;
                 }
             }
 
             /// 
             ///Clothing price变动事件
             /// 
             public event EventHandler PriceRiseHandler;
 
         }
 
         /// 
         ///Clothing price event parameters usually encapsulate a parameter type for a specific event
         /// 
         public class PriceEventArgs : EventArgs
         {
             public double OldPrice { get; set; }
             public double NewPrice { get; set; }
         }
 
         public class TaoBao {
             /// 
             ///Taobao subscribers
             /// 
             public void PublishPriceInfo(object sender, EventArgs e) {
                 Clothes clothes = (Clothes)sender;
                 PriceEventArgs args = (PriceEventArgs)e;
                 if (args.NewPrice < args.OldPrice)
                     Console.WriteLine ($"Taobao: announce the decrease of clothing price{ clothes.Name }Clothing down{ args.OldPrice  -  args.NewPrice }Yuan, buy in time! ");
                 else 
                     Console.WriteLine ("Taobao: quietly rising prices or unchanged prices, do nothing");
             }
 
         }
 
         public class Consumer
         {
             /// 
             ///Consumer subscribers
             /// 
             public void Buy(object sender, EventArgs e)
             {
                 Clothes clothes = (Clothes)sender;
                 PriceEventArgs args = (PriceEventArgs)e;
                 if (args.NewPrice < args.OldPrice)
                     Console.WriteLine ($"consumer: previous price{ args.OldPrice }, current price{ args.NewPrice }I bought it decisively! ");
                 else 
                     Console.WriteLine ($"consumer: wait and see, let's talk about the price reduction");
             }
         }
 
         public static void Show()
         {
             Clothes  clothes = new Clothes()
             {
                 Id = "12111-XK",
                 Name = UNIQLO,
                 Price = 128
             };
             //Subscribe: Associate subscriber and publisher events
             clothes.PriceRiseHandler += new TaoBao().PublishPriceInfo;
             clothes.PriceRiseHandler += new Consumer().Buy;
             //Price changes automatically trigger subscriber subscription events
             clothes.Price = 300;
         }
 
     }

call

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

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

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]