/// <summary> /// 观察者模式中的推拉,类似于winform窗体事件 /// </summary> /// <param name="args"></param> private static void MainOberser(string[] args) { door door = new door(); Doorbell doorbell = new Doorbell(); Photograph photograph = new Photograph(); Whistle whistle = new Whistle(); SendMessagePhone sendMessagePhone = new SendMessagePhone(); door.Register(photograph); door.Register(whistle); door.Register(sendMessagePhone); //doorbell.Register(photograph); //doorbell.Register(sendMessagePhone); //doorbell.Press(); door.IsAlarm = true; door.OpenedDoor(); door.IsAlarm = false; door.OpenedDoor(); door.UnRegister(whistle); door.IsAlarm = true; door.OpenedDoor(); Console.ReadLine(); }
public void Notify(IObserverble observerble, EventArgs eventArgs) { // 解耦不够彻底 // 这里存在一个向下转换(由继承体系中高级别的类向低级别的类转换)。 door door = (door)observerble; Console.WriteLine($"I am SendMessagePhone,the opendoor time {door._TimeNow}"); }