static void Main(string[] args)
        {
            Console.WriteLine(" ***Observer Pattern Demo***\n");
            //We have 3 observers- 2 of them are ObserverType1, 1 of them is of ObserverType2
            IObserver myObserver1 = new ObserverType1("Roy");
            IObserver myObserver2 = new ObserverType1("Kevin");
            IObserver myObserver3 = new ObserverType2("Bose");
            Subject   subject     = new Subject();

            //Registering the observers-Roy,Kevin,Bose
            subject.Register(myObserver1);
            subject.Register(myObserver2);
            subject.Register(myObserver3);
            Console.WriteLine(" Setting Flag = 5 ");
            subject.Flag = 5;
            //Unregistering an observer(Roy))
            subject.Unregister(myObserver1);
            //No notification this time Roy.Since it is unregistered.
            Console.WriteLine("\n Setting Flag = 50 ");
            subject.Flag = 50;
            //Roy is registering himself again
            subject.Register(myObserver1);
            Console.WriteLine("\n Setting Flag = 100 ");
            subject.Flag = 100;
            Console.ReadKey();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Observer Pattern");
            //we have 4 observers - 2 of them are ObserverType1 and 2 are ObserverType2

            var myObserver1 = new ObserverType1("Roy");
            var myObserver2 = new ObserverType1("Kevin");
            var myObserver3 = new ObserverType2("Bose");
            var myObserver4 = new ObserverType2("Jacklin");

            Console.WriteLine("Working with first celebrity now!");

            ICelebrity celebrity = new Celebrity("Celebrity-1");

            //registering the observers - Roy, Kevin, Bose

            celebrity.Register(myObserver1);
            celebrity.Register(myObserver2);
            celebrity.Register(myObserver3);

            Console.WriteLine("Celebrity-1 is setting Flag = 5");

            celebrity.Flag = 5;

            //Kevin doesn't want to get further notification
            //unregistering the observer(Kevin)

            Console.WriteLine("Celebrity-1 is removing Kevin from the observer list now");
            celebrity.Unregister(myObserver2);

            Console.WriteLine("Celebrity-1 is setting Flag = 50");
            celebrity.Flag = 50;

            //Kevin is registering himself again
            celebrity.Register(myObserver2);

            Console.WriteLine("Celebrity-1 is setting Flag = 100");
            celebrity.Flag = 100;

            Console.WriteLine("Working with another celebrity now!");
            ICelebrity celebrity2 = new Celebrity("Celebrity-2");

            //registering the observers Roy and Jacklin
            celebrity2.Register(myObserver1);
            celebrity2.Register(myObserver4);

            Console.WriteLine("Celebrity-2 is setting Flag = 500");
            celebrity2.Flag = 500;
            Console.Read();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***Observer Pattern Demonstration.***\n");
            //We have 4 observers-2 of them are ObserverType1, 1  is of ObserverType2
            IObserver myObserver1 = new ObserverType1("Roy");
            IObserver myObserver2 = new ObserverType1("Kevin");
            IObserver myObserver3 = new ObserverType2("Bose");
            IObserver myObserver4 = new ObserverType2("Jacklin");

            Console.WriteLine("Working with first celebrity now.");
            ICelebrity celebrity = new Celebrity("Celebrity-1");

            //Registering the observers-Roy,Kevin,Bose
            celebrity.Register(myObserver1);
            celebrity.Register(myObserver2);
            celebrity.Register(myObserver3);
            Console.WriteLine(" Celebrity-1 is setting Flag = 5.");
            celebrity.Flag = 5;

            /*
             * Kevin doesn't want to get further notification.
             * So, unregistering the observer(Kevin)).
             */
            Console.WriteLine("\nCelebrity-1 is removing Kevin from the observer list now.");
            celebrity.Unregister(myObserver2);
            //No notification is sent to Kevin this time. He has unregistered.
            Console.WriteLine("\n Celebrity-1 is setting Flag = 50.");
            celebrity.Flag = 50;
            //Kevin is registering himself again
            celebrity.Register(myObserver2);
            Console.WriteLine("\n Celebrity-1 is setting Flag = 100.");
            celebrity.Flag = 100;

            Console.WriteLine("\n Working with another celebrity now.");
            //Creating another celebrity
            ICelebrity celebrity2 = new Celebrity("Celebrity-2");

            //Registering the observers-Roy and Jacklin
            celebrity2.Register(myObserver1);
            celebrity2.Register(myObserver4);
            Console.WriteLine("\n --Celebrity-2 is setting Flag value as 500.--");
            celebrity2.Flag = 500;

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Observer Pattern");

            IObserver obs1 = new ObserverType1("Jessie");
            IObserver obs2 = new ObserverType2("Luke");
            IObserver obs3 = new ObserverType1("Bo");

            Subject sbj = new Subject();

            sbj.Register(obs1);
            sbj.Register(obs2);
            sbj.Register(obs3);

            sbj.Flag = 50;

            sbj.UnRegister(obs1);

            sbj.Flag = 100;
        }