protected virtual void Notify(BoiledEventArgs e) { foreach (IObserver observer in container) { observer.Update(e); } }
protected virtual void OnBoiled(BoiledEventArgs e) { base.Notify(e); }
private bool isDisplayedType = false; // 标记变量,标示是否已经打印过 #endregion Fields #region Methods public void Update(BoiledEventArgs e) { // 打印产地和型号,只打印一次 if (!isDisplayedType) { Console.WriteLine("{0} - {1}: ", e.Area, e.Type); Console.WriteLine(); isDisplayedType = true; } if (e.Temperature < 100) { Console.WriteLine( String.Format("Alarm".PadRight(7) + ":水快烧开了,当前温度:{0}。", e.Temperature)); } else { Console.WriteLine( String.Format("Alarm".PadRight(7) + ":水已经烧开了!!")); } }
public void BoilWater() { for (int i = 0; i < 99; i++) { temprature = i + 1; if (temprature > 97) { BoiledEventArgs e = new BoiledEventArgs(temprature, type, area); OnBoiled(e); } } }