/// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            // 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();
        }
示例#2
0
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            public static void Main()
            {
                // 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();
            }
示例#3
0
        public void Test()
        {
            var subject = new ConcreteSubject();

            var observer1 = new ConcreteObserver1();
            var observer2 = new ConcreteObserver2();

            subject.Register(observer1);
            subject.Register(observer2);
            Assert.That(subject.Property1, Is.Null);
            Assert.That(observer1.Property1, Is.Null);
            Assert.That(subject.Property2, Is.Null);
            Assert.That(observer2.Property2, Is.Null);

            subject.Property1 = "property 1";
            Assert.That(subject.Property1, Is.EqualTo("property 1"));
            Assert.That(observer1.Property1, Is.EqualTo("property 1"));
            Assert.That(subject.Property2, Is.Null);
            Assert.That(observer2.Property2, Is.Null);

            subject.Property2 = "property 2";
            Assert.That(subject.Property1, Is.EqualTo("property 1"));
            Assert.That(observer1.Property1, Is.EqualTo("property 1"));
            Assert.That(subject.Property2, Is.EqualTo("property 2"));
            Assert.That(observer2.Property2, Is.EqualTo("property 2"));

            subject.Unregister(observer1);
            subject.Property1 = "PROPERTY 1";
            Assert.That(subject.Property1, Is.EqualTo("PROPERTY 1"));
            Assert.That(observer1.Property1, Is.EqualTo("property 1"));
            Assert.That(subject.Property2, Is.EqualTo("property 2"));
            Assert.That(observer2.Property2, Is.EqualTo("property 2"));

            subject.Property2 = "PROPERTY 2";
            Assert.That(subject.Property1, Is.EqualTo("PROPERTY 1"));
            Assert.That(observer1.Property1, Is.EqualTo("property 1"));
            Assert.That(subject.Property2, Is.EqualTo("PROPERTY 2"));
            Assert.That(observer2.Property2, Is.EqualTo("PROPERTY 2"));
        }
示例#4
0
 public ConcreteObserver(ConcreteSubject subject, string name)
 {
     Subject = subject;
     _name   = name;
 }
示例#5
0
 public ConcreteObserver(ConcreteSubject subject, string name)
 {
     this._subject = subject;
     this._name    = name;
 }
 // Constructor
 public ConcreteObserver(
   ConcreteSubject subject, string name)
 {
     this._subject = subject;
     this._name = name;
 }