コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Observer Pattern example");
            Console.WriteLine("------------------------");

            var consoleClocks = new List<IConsoleClock>();
            var clockSubject = new ClockSubject();

            consoleClocks.Add(new GMTConsoleClock(clockSubject));
            consoleClocks.Add(new SecondsCountingConsoleClock(clockSubject));

            var isRunning = true;
            while (isRunning)
            {
                Console.WriteLine();
                Console.WriteLine("Setting Time to now...");
                clockSubject.SetState(new ClockState());

                Console.WriteLine("Printing updated values from Clock Observers...");

                foreach (var clock in consoleClocks)
                {
                    clock.WriteStateToConsole();
                }

                Console.WriteLine();
                Console.WriteLine("X key to exit, any key to repeat.");
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.X)
                {
                    isRunning = false;
                }
            }

        }
コード例 #2
0
        public GMTConsoleClock(ClockSubject clockSubject)
        {
            // register with the clock subject for updates
            this.clockSubject = clockSubject;

            clockSubject.Attach(this);
        }
コード例 #3
0
        public SecondsCountingConsoleClock(ClockSubject clockSubject)
        {
            // register with the clock subject for updates
            this.clockSubject = clockSubject;

            clockSubject.Attach(this);
        }