Beispiel #1
0
        private void Simulate(AbstractDuckFactory duckFactory, AbstractGooseFactory gooseFactory)
        {
            //IQuackable _mallardDuck = duckFactory.CreateMallardDuck();
            IQuackable _redheadDuck = duckFactory.CreateRedheadDuck();
            IQuackable _duckCall    = duckFactory.CreateDuckCall();
            IQuackable _rubberDuck  = duckFactory.CreateRubberDuck();
            IQuackable _gooseDuck   = gooseFactory.CreateGoose();

            Flock flockOfDucks = new Flock();

            //flockOfDucks.Add(_mallardDuck);
            flockOfDucks.Add(_redheadDuck);
            flockOfDucks.Add(_duckCall);
            flockOfDucks.Add(_rubberDuck);
            flockOfDucks.Add(_gooseDuck);

            Flock flockOfMallards = new Flock();

            IQuackable mallardOne   = duckFactory.CreateMallardDuck();
            IQuackable mallardTwo   = duckFactory.CreateMallardDuck();
            IQuackable mallardThree = duckFactory.CreateMallardDuck();
            IQuackable mallardFour  = duckFactory.CreateMallardDuck();

            //IQuackable mallardFive = duckFactory.CreateMallardDuck();

            flockOfMallards.Add(mallardOne);
            flockOfMallards.Add(mallardTwo);
            flockOfMallards.Add(mallardThree);
            flockOfMallards.Add(mallardFour);
            //flockOfMallards.Add(mallardFive);

            flockOfDucks.Add(flockOfMallards);


            Console.WriteLine("Duck Simulator\nFull Flock");

            Simulate(flockOfDucks);

            Console.WriteLine("Duck Simulator\nMallard Flock");

            Simulate(flockOfMallards);

            Console.WriteLine("Value:" + QuackCounter.NumberOfQuacks);

            Quackologist quackologist = new Quackologist();

            flockOfDucks.RegisterObserver(quackologist);

            Simulate(flockOfDucks);
            Console.WriteLine("Утки крякнут " + QuackCounter.NumberOfQuacks + " раз");
        }
        private static void Simulate(AbstractDuckFactory duckFactory, AbstractGooseFactory gooseFactory)
        {
            IQuackable mallardDuck  = duckFactory.CreateMallardDuck();
            IQuackable readheadDuck = duckFactory.CreateReadheadDuck();
            IQuackable duckCall     = duckFactory.CreateDuckCall();
            IQuackable rubberDuck   = duckFactory.CreateRubberDuck();
            IQuackable gooseDuck    = new GooseAdapter(gooseFactory.CreateGoose());

            Console.WriteLine("\nDuck Simulator: With Composite - Flocks");

            var flockOfDucks = new Flock();

            flockOfDucks.Add(mallardDuck);
            flockOfDucks.Add(readheadDuck);
            flockOfDucks.Add(duckCall);
            flockOfDucks.Add(rubberDuck);
            flockOfDucks.Add(gooseDuck);

            var flockOfMallards = new Flock();

            IQuackable mallard_1 = duckFactory.CreateMallardDuck();
            IQuackable mallard_2 = duckFactory.CreateMallardDuck();
            IQuackable mallard_3 = duckFactory.CreateMallardDuck();
            IQuackable mallard_4 = duckFactory.CreateMallardDuck();

            flockOfMallards.Add(mallard_1);
            flockOfMallards.Add(mallard_2);
            flockOfMallards.Add(mallard_3);
            flockOfMallards.Add(mallard_4);

            flockOfDucks.Add(flockOfMallards);

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

            flockOfDucks.RegisterObserver(quackologist);

            Console.WriteLine("\nDuck Simulator: Whole Flock Simulation");
            Simulate(flockOfDucks);

            Console.WriteLine("\nDuck Simulator: Mallard Flock Simulation");
            Simulate(flockOfMallards);

            Console.WriteLine($"The ducks quacked {QuackCounter.GetQuacks()} times");
        }
Beispiel #3
0
        private static void generateDucksAndRunSimulation(AbstractDuckFactory abstractDuckFactory, AbstractGooseFactory abstractGooseFactory)
        {
            IQuackable redheadDuck = abstractDuckFactory.CreateRedheadDuck();
            IQuackable duckCall    = abstractDuckFactory.CreateDuckCall();
            IQuackable rubberDuck  = abstractDuckFactory.CreateRubberDuck();

            IQuackable gooseDuck = abstractGooseFactory.CreateGoose();

            Flock flockOfDucks = new Flock();

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

            Flock flockOfMallards = new Flock();

            // Create a little family of mallards here
            IQuackable mallardDuck1 = abstractDuckFactory.CreateMallardDuck();
            IQuackable mallardDuck2 = abstractDuckFactory.CreateMallardDuck();
            IQuackable mallardDuck3 = abstractDuckFactory.CreateMallardDuck();
            IQuackable mallardDuck4 = abstractDuckFactory.CreateMallardDuck();

            flockOfMallards.Add(mallardDuck1);
            flockOfMallards.Add(mallardDuck2);
            flockOfMallards.Add(mallardDuck3);
            flockOfMallards.Add(mallardDuck4);

            // Also add the flock of mallards to the flock of ducks - They are ducks in the end too!
            flockOfDucks.Add(flockOfMallards);

            Console.WriteLine("\nDuck Simulator: Whole Flock Simulation with observer");
            IObserver quackologist = new Quackologist();

            flockOfDucks.RegisterObserver(quackologist);
            simulate(flockOfDucks);

            Console.WriteLine("\nDuck Simulator: Mallard Flock Simulation");
            simulate(flockOfMallards);

            Console.WriteLine("The ducks quacked " + QuackCounterDecorator.GetNumberOfQuacks() + " times");
        }