public void Simulate(AbstractDuckFactory duckFactory)
        {
            IQuackable mallardDuck   = duckFactory.CreateMallardDuck();
            IQuackable redheadDuck   = duckFactory.CreateRedheadDuck();
            IQuackable duckCall      = duckFactory.CreateDuckCall();
            IQuackable rubberDuck    = duckFactory.CreateRubberDuck();
            IQuackable quackingGoose = new GooseAdapter(new Goose());

            Flock flockOfDucks = new Flock();

            flockOfDucks.Add(mallardDuck);
            flockOfDucks.Add(redheadDuck);
            flockOfDucks.Add(duckCall);
            flockOfDucks.Add(rubberDuck);

            Console.WriteLine("Separate Duck Simulation");
            Simulate(mallardDuck);
            Simulate(redheadDuck);
            Simulate(duckCall);
            Simulate(rubberDuck);
            Simulate(quackingGoose);

            Quackologist quackologist = new Quackologist();

            flockOfDucks.RegisterObserver(quackologist);

            Console.WriteLine("Flock of Ducks simulation");
            Simulate(flockOfDucks);

            Console.WriteLine("Quack counter : {0}", QuackCounter.QuackCount);
            Console.ReadLine();
        }
Exemple #2
0
        public void Simulate(AbstractDuckFactory duckFactory)
        {
            IQuackable mallardDuck = duckFactory.CreateMallardDuck();
            IQuackable redHeadDuck = duckFactory.CreateRedHeadDuck();
            IQuackable duckCall    = duckFactory.CreateDuckCall();
            IQuackable rubberDuck  = duckFactory.CreateRubberDuck();

            Goose      goose = new GooseFactory().CreateGoose();
            IQuackable goosePretendingToBeADuck = new GooseAdapter(goose);

            Flock flockOfDucks = new Flock();

            flockOfDucks.Add(mallardDuck);
            flockOfDucks.Add(redHeadDuck);
            flockOfDucks.Add(duckCall);
            flockOfDucks.Add(rubberDuck);

            Flock flockOfMallardDucks = new Flock();

            flockOfMallardDucks.Add(duckFactory.CreateMallardDuck());
            flockOfMallardDucks.Add(duckFactory.CreateMallardDuck());
            flockOfMallardDucks.Add(duckFactory.CreateMallardDuck());
            flockOfMallardDucks.Add(duckFactory.CreateMallardDuck());
            flockOfMallardDucks.Add(duckFactory.CreateMallardDuck());
            flockOfMallardDucks.Add(duckFactory.CreateMallardDuck());

            flockOfDucks.Add(flockOfMallardDucks);

            IQuackObserver quackScientist = new Quackologist();

            flockOfDucks.RegisterObserver(quackScientist);

            Console.WriteLine("\nDuck Simulator:");

            //Console.WriteLine("Flock of mallard ducks: ");
            //Simulate(flockOfMallardDucks);

            Console.WriteLine("\nFlock of all ducks: ");
            Simulate(flockOfDucks);

            Console.WriteLine("\nThe goose pretending to be duck: ");
            Simulate(goosePretendingToBeADuck);

            Console.WriteLine($"\nThe ducks quacked {QuackCounter.GetQuacks()} times");
        }
        void Simulate(AbstractDuckFactory duckFactory, AbstractGooseFactory gooseFactory)
        {
            Console.WriteLine("Duck Simulator: With Compsoite - Flock");

            IQuackable redheadDuck = duckFactory.CreateRedheadDuck();
            IQuackable duckCall    = duckFactory.CreateDuckCall();
            IQuackable rubberDuck  = duckFactory.CreateRubberDuck();
            IQuackable gooseDuck   = gooseFactory.CreateGoose();

            Flock flockOfDucks = new Flock();

            flockOfDucks.Add(redheadDuck);
            flockOfDucks.Add(duckCall);
            flockOfDucks.Add(rubberDuck);
            flockOfDucks.Add(gooseDuck);

            IQuackable mallardDuckOne   = duckFactory.CreateMallardDuck();
            IQuackable mallardDuckTwo   = duckFactory.CreateMallardDuck();
            IQuackable mallardDuckThree = duckFactory.CreateMallardDuck();
            IQuackable mallardDuckFour  = duckFactory.CreateMallardDuck();

            Flock flockOfMallards = new Flock();

            flockOfMallards.Add(mallardDuckOne);
            flockOfMallards.Add(mallardDuckTwo);
            flockOfMallards.Add(mallardDuckThree);
            flockOfMallards.Add(mallardDuckFour);

            flockOfDucks.Add(flockOfMallards);

            Console.WriteLine("\nDuck Simulator: With Observer");
            Quackologist quackologist = new Quackologist();

            flockOfDucks.RegisterObserver(quackologist);
            Simulate(flockOfDucks);

            Console.WriteLine($"\nThe ducks quacked {QuackCounterDecorator.NumberOfQuacks} times");
        }