示例#1
0
        static void Main(string[] args)
        {
            ConcreteSubject   subject    = new ConcreteSubject();
            ConcreteObserverA observerA1 = new ConcreteObserverA(subject, "observer1");
            ConcreteObserverA observerA2 = new ConcreteObserverA(subject, "observer2");

            subject.Attach(observerA1);
            subject.Attach(observerA2);
            subject.SubjectState = "Hello";
            subject.Notify();
            subject.SubjectState = "World";
            subject.Notify();
        }
示例#2
0
        public void TestMethod1()
        {
            ConcreteSubject subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject, "Observer 1"));
            subject.Attach(new ConcreteObserver(subject, "Observer 2"));
            subject.Attach(new ConcreteObserver(subject, "Observer 3"));

            subject.State = "State 1";
            subject.Notify();

            subject.State = "State 2";
            subject.Notify();
        }
示例#3
0
        /// <summary>

        /// Entry point into console application.

        /// </summary>

        public static void Demonstrate()
        {
            // Configure Observer pattern

            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "X"));
            s.Attach(new ConcreteObserver(s, "Y"));
            s.Attach(new ConcreteObserver(s, "Z"));

            // Change subject and notify observers

            s.SubjectState = "ABC";
            s.Notify();

            // Wait for user

            Console.ReadKey();
        }