Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            #region
            ////Cat Mouse Owner and child

            //Cat cat = new Cat("Tom");

            //Observer mouse = new Mouse("Jerry", cat);

            //Observer master = new Master(cat);

            //Observer1 baby = new Baby(cat);

            //cat.Cry();

            //Console.Read();

            #endregion

            #region For observer pattern structure
            ConcreteSubject subject = new ConcreteSubject();

            subject.Attach(new ConcreteObserver(subject, "Observer A"));
            subject.Attach(new ConcreteObserver(subject, "Observer B"));
            subject.Attach(new ConcreteObserver(subject, "Observer C"));

            subject.SubjectState = "Ready";
            subject.Notify();

            #endregion

            Console.Read();
        }
Ejemplo n.º 2
0
        public static void Test()
        {
            var concreteSubject = new ConcreteSubject("state 1");

            concreteSubject.Attach(new ConcreteObserver("1"));
            concreteSubject.Attach(new ConcreteObserver("2"));

            concreteSubject.State = "state 2";
            concreteSubject.State = "state 3";
        }
Ejemplo n.º 3
0
        //观察者模式 [əb'zə:və, ɔb-]
        static void Main(string[] args)
        {
            ConcreteSubject s = new ConcreteSubject();
            s.Attach(new ConcreteObserver(s, "a"));
            s.Attach(new ConcreteObserver(s, "b"));
            s.Attach(new ConcreteObserver(s, "c"));

            s.SubjectState = "run";
            s.Notify();
            Console.Read();
        }
Ejemplo n.º 4
0
        public void TestMethod1()
        {
            var subject   = new ConcreteSubject();
            var observer1 = new ConcreteObserver(subject);
            var observer2 = new ConcreteObserver(subject);

            subject.Attach(observer1);
            subject.Attach(observer2);

            subject.State = "NEW";

            subject.Notify();

            Assert.AreEqual(subject.State, observer1.State);
            Assert.AreEqual(subject.State, observer2.State);
        }
Ejemplo n.º 5
0
        public static void Main_Observer()
        {
            var sub1 = new ConcreteSubject();

            var ob1 = new ConcreteObserver1();
            var ob2 = new ConcreteObserver1();

            //添加观察者
            sub1.Add(ob1);
            sub1.Add(ob2);

            sub1.Notify("New Msg");
            sub1.Renmove(ob2);

            Console.ReadLine();
        }
Ejemplo n.º 6
0
    /// <summary>
    /// 观察者模式的 客户端代码
    /// </summary>
    void Observer_main()
    {
        ///声明一个新的主题对象
        ConcreteSubject s = new ConcreteSubject();

        // 为主题对象  增加观察者
        s.Attach(new ConcreteObserver(s, "X"));
        s.Attach(new ConcreteObserver(s, "Y"));
        s.Attach(new ConcreteObserver(s, "Z"));

        ///更新主题的 状态
        s.SubjectState = "ABC";
        s.Notify();

        Debug.Log("通知成功");
    }
Ejemplo n.º 7
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Demo()
        {
            // 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();
        }
Ejemplo n.º 8
0
    void Start()
    {
        // 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();
        // Change subject and notify observers again
        s.SubjectState = "666";
        s.Notify();
    }
Ejemplo n.º 9
0
            public static void Observer()
            {
                ConcreteSubject cs = new ConcreteSubject();

                ConcreteObserver co1 = new ConcreteObserver("Observer 1");
                ConcreteObserver co2 = new ConcreteObserver("Observer 2");
                ConcreteObserver co3 = new ConcreteObserver("Observer 3");

                cs.Add(co1);
                cs.Add(co2);
                cs.Add(co3);

                cs.Notify();

                Console.Read();
            }
Ejemplo n.º 10
0
        // "Subject"

        // "ConcreteSubject"
        public void TestObserverPattern()
        {
            // Configure Observer pattern
            var 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.Read();
        }
Ejemplo n.º 11
0
	// Update is called once per frame
	void UnitTest () 
	{
		// 主題
		ConcreteSubject theSubject = new ConcreteSubject();

		// 加入觀察者
		ConcreteObserver1 theObserver1 = new ConcreteObserver1(theSubject);
		theSubject.Attach( theObserver1 );
		theSubject.Attach( new ConcreteObserver2(theSubject) );

		// 設定Subject
		theSubject.SetState("Subject狀態1");

		// 顯示狀態
		theObserver1.ShowState();	
	}
Ejemplo n.º 12
0
        /// <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();
        }
Ejemplo n.º 13
0
    // Update is called once per frame
    void UnitTest()
    {
        // 主题
        ConcreteSubject theSubject = new ConcreteSubject();

        // 加入观察者
        ConcreteObserver1 theObserver1 = new ConcreteObserver1(theSubject);

        theSubject.Attach(theObserver1);
        theSubject.Attach(new ConcreteObserver2(theSubject));

        // 设定Subject
        theSubject.SetState("Subject状态1");

        // 显示状态
        theObserver1.ShowState();
    }
Ejemplo n.º 14
0
        public void Start()
        {
            // Configure Observer pattern

            ConcreteSubject s = new ConcreteSubject();

            s.Attach(new ConcreteObserver(s, "Washing machine"));
            s.Attach(new ConcreteObserver(s, "Coffer maker"));

            // Change subject and notify observers
            System.Console.WriteLine("Notify observers");
            s.SubjectState = "Start";
            s.Notify();

            s.SubjectState = "Stop";
            s.Notify();
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            //IBM myIBM = new IBM("IBM", 120.00);
            //myIBM.Attach(new Investor("Sorres"));
            //myIBM.Attach(new Investor("Berkeley"));

            //myIBM.Price = 120.10;
            //myIBM.Price = 122;
            //myIBM.Price = 130;

            ConcreteSubject myConcreteSubject = new ConcreteSubject();
            myConcreteSubject.Attach(new ConcreteObserver("Hans", myConcreteSubject));
            myConcreteSubject.Attach(new ConcreteObserver("Jokke", myConcreteSubject));

            myConcreteSubject.State = "Hello";
            myConcreteSubject.State = "Derp";
        }
Ejemplo n.º 16
0
    // Update is called once per frame
    void UnitTest()
    {
        // 主題
        ConcreteSubject theSubject = new ConcreteSubject();

        // 加入觀察者
        ConcreteObserver1 theObserver1 = new ConcreteObserver1(theSubject);

        theSubject.Attach(theObserver1);
        theSubject.Attach(new ConcreteObserver2(theSubject));

        // 設定Subject
        theSubject.SetState("Subject狀態1");

        // 顯示狀態
        theObserver1.ShowState();
    }
Ejemplo n.º 17
0
        public void Run()
        {
            ConcreteSubject subject   = new ConcreteSubject();
            Observer        observer1 = new ConcreteObserver(subject, "X");
            Observer        observer2 = new ConcreteObserver(subject, "Y");
            Observer        observer3 = new ConcreteObserver(subject, "Z");

            subject.RegisterObservers(observer1, observer2, observer3);

            // Change subject and notify observers
            subject.SubjectState = "ABC";
            subject.UnRegisterObservers(observer1, observer3);
            subject.SubjectState = "XYZ";

            Console.WriteLine("\nImplementation using .NET IObserver and IObservable");
            DotNetImplementation.NetObserver.Run();
        }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            #region 抽象模式
            ConcreteSubject s = new ConcreteSubject();

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

            s.SubjectState = "ABC";
            //发出通知
            s.Notify();

            #endregion

            Console.ReadKey();
        }
Ejemplo n.º 19
0
        public void Start()
        {
            var subject = new ConcreteSubject();

            var firstConcreteObserver  = new ConcreteObserver(subject, "First");
            var secondConcreteObserver = new ConcreteObserver(subject, "Second");

            subject.Attach(firstConcreteObserver);
            subject.Attach(secondConcreteObserver);

            subject.SubjectState = "FirstState";
            subject.Notify();

            subject.Detach(secondConcreteObserver);

            subject.SubjectState = "SecondState";
            subject.Notify();
        }
Ejemplo n.º 20
0
        static void Main(string[] args)
        {
            //IBM myIBM = new IBM("IBM", 120.00);
            //myIBM.Attach(new Investor("Sorres"));
            //myIBM.Attach(new Investor("Berkeley"));

            //myIBM.Price = 120.10;
            //myIBM.Price = 122;
            //myIBM.Price = 130;

            ConcreteSubject myConcreteSubject = new ConcreteSubject();

            myConcreteSubject.Attach(new ConcreteObserver("Hans", myConcreteSubject));
            myConcreteSubject.Attach(new ConcreteObserver("Jokke", myConcreteSubject));

            myConcreteSubject.State = "Hello";
            myConcreteSubject.State = "Derp";
        }
Ejemplo n.º 21
0
        static void Main(string[] args)
        {
            ConcreteSubject subj = new ConcreteSubject();

            subj.Attach(new ConcreteObserver(subj));
            subj.Attach(new ConcreteObserver(subj));

            subj.State = "Some state";
            subj.Notify();

            ConcreteSubject1 subj1 = new ConcreteSubject1();

            subj1.Attach(new ConcreteObserver2(subj1));
            subj1.Attach(new ConcreteObserver2(subj1));

            subj1.State = "Some state 1";
            subj1.Notify();
        }
Ejemplo n.º 22
0
        void TestDP09ObserverDesignPattern()
        {
            ConcreteSubject sub = new ConcreteSubject();

            IObserver observer1 = new ConcreteObserver1("订阅者1");

            observer1.Subscribe(sub);
            sub.Update("周一新爆料");


            IObserver observer2 = new ConcreteObserver2("订阅者2");

            observer2.Subscribe(sub);
            sub.Update("周二新爆料");

            observer1.Unsubscribe(sub);
            sub.Update("周五新爆料");
        }
Ejemplo n.º 23
0
    public static void Main()
    {
        var subject   = new ConcreteSubject();
        var observers = new[] { new ConcreteObserver(), new ConcreteObserver(), new ConcreteObserver() };

        foreach (var observer in observers)
        {
            subject.Attach(observer);
        }
        Assert.All(observers, (el => el.State == null));
        subject.SubjectState = "init";
        subject.Notify();
        Assert.All(observers, (el => "init".Equals(el.State)));
        subject.SubjectState = "changed";
        subject.Notify();
        Assert.All(observers, (el => "changed".Equals(el.State)));
        TestRunner.Print();
    }
Ejemplo n.º 24
0
        private static void Main()
        {
            using (var subject = new ConcreteSubject())
            {
                var observer1 = new ConcreteObserver("observer1");
                var observer2 = new ConcreteObserver("observer2");

                using (subject.Subscribe(observer1))
                    using (subject.Subscribe(observer2))
                    {
                        subject.SubjectState = "Modified";
                    }

                subject.SubjectState = "Modified Again !";
            }

            Console.ReadKey();
        }
Ejemplo n.º 25
0
        static void MainABC(string[] args)
        {
            // The client code.
            var subject   = new ConcreteSubject();
            var observerA = new ConcreteObserverA();

            subject.Attach(observerA);

            var observerB = new ConcreteObserverB();

            subject.Attach(observerB);

            subject.SomeBusinessLogic();
            subject.SomeBusinessLogic();

            subject.Detach(observerB);

            subject.SomeBusinessLogic();
        }
Ejemplo n.º 26
0
        public ObserverPattern()
        {
            ConcreteSubject subject = new ConcreteSubject();

            ConcreteObserver observer1 = new ConcreteObserver("Rome", subject);
            ConcreteObserver observer2 = new ConcreteObserver("Paris", subject);
            ConcreteObserver observer3 = new ConcreteObserver("London", subject);

            subject.Attach(observer1);
            subject.Attach(observer2);
            subject.Attach(observer3);

            for (int i = 0; i < 10; i++)
            {
                subject.ChangeState($"Status #{i}");
                subject.Notify();
                Thread.Sleep(500);
            }
        }
Ejemplo n.º 27
0
        static void Main(string[] args)
        {
            var concreteSubject = new ConcreteSubject();
            var observer1       = new ConcreteObserver("Observer 1");
            var observer2       = new ConcreteObserver("Observer 2");

            concreteSubject.ConcreteSubjectState = 2;

            concreteSubject.Attach(observer1);
            observer1.Update(concreteSubject);

            concreteSubject.Attach(observer2);
            observer2.Update(concreteSubject);

            concreteSubject.ConcreteSubjectState = 3;

            concreteSubject.Detach(observer1);

            concreteSubject.ConcreteSubjectState = 4;
        }
Ejemplo n.º 28
0
        public void SendSignal_WithSomeClient_MustUpdateAll()
        {
            var testSubject = new ConcreteSubject()
            {
                SubjectState = "signal1"
            };
            var observer1 = new ConcreteObserver(testSubject, "observer1");
            var observer2 = new ConcreteObserver(testSubject, "observer2");
            var observer3 = new ConcreteObserver(testSubject, "observer3");

            testSubject.Attach(observer1);
            testSubject.Attach(observer2);
            testSubject.Attach(observer3);

            testSubject.Notify();

            Assert.Equal("signal1", observer1.GetState());
            Assert.Equal("signal1", observer2.GetState());
            Assert.Equal("signal1", observer2.GetState());
        }
        public static void Main(string[] args)
        {
            ConcreteSubject s = new ConcreteSubject();

            s.Notify += new ConcreteObserver()
            {
                Name = "A"
            }.Update;
            s.Notify += new ConcreteObserver()
            {
                Name = "B"
            }.Update;
            s.Notify += new ConcreteObserver()
            {
                Name = "C"
            }.Update;
            s.SubjectState = "How old are you ?";

            Console.ReadLine();
        }
        public static void Main(string[] args)
        {
            ConcreteSubject s = new ConcreteSubject();

            s.Notify += new ConcreteObserver()
            {
                Name = "A"
            }.Update;
            s.Notify += new ConcreteObserver()
            {
                Name = "B"
            }.Update;
            s.Notify += new ConcreteObserver()
            {
                Name = "C"
            }.Update;
            s.SubjectState = "All I have to do is dream.";

            Console.ReadLine();
        }
Ejemplo n.º 31
0
        static void Main(string[] args)
        {
            //Create a Product with Out Of Stock Status
            ConcreteSubject RedMI = new ConcreteSubject("Red MI Mobile", 10000, "Out Of Stock");

            //User Anurag will be created and user1 object will be registered to the subject
            ConcreteObserver user1 = new ConcreteObserver("Anurag", RedMI);

            //User Pranaya will be created and user1 object will be registered to the subject
            ConcreteObserver user2 = new ConcreteObserver("Pranaya", RedMI);

            //User Priyanka will be created and user3 object will be registered to the subject
            ConcreteObserver user3 = new ConcreteObserver("Priyanka", RedMI);

            Console.WriteLine("Red MI Mobile current state : " + RedMI.GetAvailability());
            Console.WriteLine();

            // Now product is available
            RedMI.SetAvailability("Available");
            Console.Read();
        }
        public static void Main(string[] args)
        {
            ConcreteSubject s = new ConcreteSubject();

            s.AddObserver(new ConcreteObserver()
            {
                Name = "A"
            });
            s.AddObserver(new ConcreteObserver()
            {
                Name = "B"
            });
            s.AddObserver(new ConcreteObserver()
            {
                Name = "C"
            });

            s.SubjectState = "Where have all the flowers gone ?";

            Console.ReadLine();
        }
Ejemplo n.º 33
0
    // Entry point into console application.
    static void Main()
    {
        // Configure Observer pattern
        ConcreteSubject s = new ConcreteSubject();

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

        // Change subject and notify observers
        s.SubjectState = "A";
        s.Notify();

        Console.WriteLine();

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

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

        // Wait for user input
        Console.ReadKey();
    }
Ejemplo n.º 34
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"));
        }
        static void Main(string[] args)
        {
            var subject   = new ConcreteSubject();
            var observer1 = new ConcreteObserver1();

            subject.Attach(observer1);

            var observer2 = new ConcreteObserver2();

            subject.Attach(observer2);

            var observer3 = new ConcreteObserver3();

            subject.Attach(observer3);


            subject.BusinessLogic();
            subject.BusinessLogic();

            subject.Detach(observer3);

            subject.BusinessLogic();
        }
Ejemplo n.º 36
0
 public ConcreteObserver(ConcreteSubject subject, string name)
 {
     this.subject = subject;
     this.name = name;
 }
Ejemplo n.º 37
0
  public static void Main( string[] args )
  {
    // Configure Observer structure
    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();
  }
Ejemplo n.º 38
0
 public ConcreteObserver(ConcreteSubject sub)
 {
     subject = sub;
 }
Ejemplo n.º 39
0
 public ConcreteObserver(ConcreteSubject subject)
 {
     _subject = subject;
 }
Ejemplo n.º 40
0
 public ConcreteObserver(ConcreteSubject subject)
 {
     _subject = subject;
 }