Example #1
0
        static void Main(string[] args)
        {
            Heater heater = new Heater();
            Screen screen = new Screen();
            Alarm  alarm  = new Alarm();

            heater.Register(screen);  // 注册显示器
            heater.Register(alarm);   // 注册热水器
            heater.BoilWater();       // 烧水
            heater.UnRegister(alarm); // 取消报警器的注册
            heater.BoilWater();       // 再次烧水
            Console.WriteLine();
            Console.WriteLine("/*************推模式********************/");
            /*************推模式********************/
            Heater2 heater2 = new Heater2();
            Screen2 screen2 = new Screen2();

            heater2.Register(screen2); // 注册显示器
            heater2.BoilWater();       // 烧水
            Console.WriteLine();
            Console.WriteLine("/*************拉模式********************/");
            /*************拉模式********************/
            Heater3 heater3 = new Heater3();
            Screen3 screen3 = new Screen3();

            heater3.Register(screen3); // 注册显示器
            heater3.BoilWater();       // 烧水
            Console.ReadKey();
        }
Example #2
0
        private bool isDisplayedType = false; // 标记变量,标示是否已经打印过
        public void Update(IObservale3 obj)
        {
            // 这里存在一个向下转换(由继承体系中高级别的类向低级别的类转换)
            Heater3 heater = (Heater3)obj;

            // 打印产地和型号,只打印一次
            if (!isDisplayedType)
            {
                Console.WriteLine("{0} - {1}: ", heater.Area, heater.Type);
                Console.WriteLine();
                isDisplayedType = true;
            }
            if (heater.Temperature < 100)
            { // 通过热水器引用heater获取温度
                Console.WriteLine(
                    String.Format("Alarm".PadRight(7) + ":水快烧开了,当前温度:{0}。",
                                  heater.Temperature));
            }
            else
            {
                Console.WriteLine(
                    String.Format("Alarm".PadRight(7) + ":水已经烧开了!!"));
            }
        }