Example #1
0
        static void Main(string[] args)
        {
            var subject = new ConcreteSubject();

            var observers = new List <IObserver>
            {
                new ConcreteObserver(subject),
                new ConcreteObserver(subject),
                new ConcreteObserver(subject)
            };

            subject.Attach(observers[0]);
            subject.Attach(observers[1]);
            subject.Attach(observers[2]);

            subject.SetState(12);

            subject.Notify();
            subject.Notify();
        }
        public void RunExample()
        {
            Console.Clear();
            Console.WriteLine("Begin vanilla observer example...\n");
            var subject = new ConcreteSubject();

            Console.WriteLine("Creating a few random observers and attaching to the subject.");

            subject.Attach(new ConcreteObserver(subject, "AbbaCabba"));
            subject.Attach(new ConcreteObserver(subject, "YaddAbba"));
            subject.Attach(new ConcreteObserver(subject, "MooNooGloo"));
            subject.Attach(new ConcreteObserver(subject, "Virus!!!!"));
            subject.Attach(new ConcreteObserver(subject, "The Last Gummy"));

            Console.WriteLine("Getting the current state of the subject.");

            subject.SubjectState = "Default State, nothing has changed yet.";

            subject.Notify();


            Console.WriteLine("Changing state...");

            Console.WriteLine("Getting the current state of the subject.");


            subject.SubjectState = "The state has changed!";

            subject.Notify();

            Console.WriteLine();

            Console.WriteLine("End vanilla observer example...\n Press any key to return to main menu.");
            Console.ReadKey();
            Console.Clear();
        }