Ejemplo n.º 1
0
        public Main()
        {
            //create the subject
            var s1 = new ConcreteSubject();

            //version 1 -
            //create the observer
            //the data is passed during notification.
            var o3 = new ConcreteObserver();
            var o4 = new ConcreteObserver();

            //version 2 -
            //create the observer and pass the subject
            //to pull the requiered data after a change.
            var o1 = new ConcreteObserver(s1);
            var o2 = new ConcreteObserver(s1);

            //Attach observers , only version 1
            s1.Add(o3);
            s1.Add(o4);

            //Perform a change of state.
            s1.ID = 1;
            s1.ID = 3;
        }
Ejemplo n.º 2
0
        private static void HandleObserver()
        {
            ISubject  subject   = new ConcreteSubject();
            IObserver observer  = new ConcreteObserver1(subject, "观察者1");
            IObserver observer2 = new ConcreteObserver1(subject, "观察者2");

            subject.Add(observer);
            subject.Add(observer2);

            subject.SutjectState = "服务器崩溃了!";
            subject.Notify();

            Console.Read();
        }
Ejemplo n.º 3
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.º 4
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();
            }