public void Simulate(AbstractDuckFactory factory)
        {
            var flockOfDucks = new Flock();

            flockOfDucks.Add(factory.CreateMallardDuck());
            flockOfDucks.Add(factory.CreateRedheadDuck());
            flockOfDucks.Add(factory.CreateDuckCall());
            flockOfDucks.Add(factory.CreateRubberDuck());
            flockOfDucks.Add(new QuackCounter(new GooseAdapter(new Goose())));

            var flockOfMallards = new Flock();

            for (int i = 0; i < 4; i++)
            {
                flockOfMallards.Add(factory.CreateMallardDuck());
            }

            flockOfDucks.Add(flockOfMallards);

            "Duck Simulator with Observer\n--------------------------------".P();
            var quackologist = new Quackologist();

            flockOfDucks.RegisterObserver(quackologist);
            flockOfDucks.Quack();
            "Mallard Simulator\n--------------------------------".P();
            flockOfMallards.Quack();
            "--------------------------------\nThe ducks quacked {0} times".P(QuackCounter.GetQuacks());
        }
Exemple #2
0
    public void simulate(AbstractDuckFactory duckFactory)
    {
        Quackable mallardDuck  = duckFactory.createMallardDuck();
        Quackable redheadDuck  = duckFactory.createRedheadDuck();
        Quackable duckCall     = duckFactory.createDuckCall();
        Quackable rubberDuck   = duckFactory.createRubberDuck();
        Goose     goose        = new Goose();
        Quackable gooseAdapter = new GooseAdapter(goose);

        Flock flockOfDucks = new Flock();

        Quackologist quackologist = new Quackologist();

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

        //foarte important sa fie dupa ce am adaugat lez ducks ,
        //inregistrarea observerului se face cu interator pt toate lez ducks existente
        flockOfDucks.registerObserver(quackologist);

        Console.WriteLine("Duck Simulator");

        simulate(flockOfDucks);

        Console.WriteLine(" The ducks quacked " + QuackCounter.getQaucks() + " times");
    }
        public void DuckSimulatorObserver()
        {
            QuackCounter.QuackCount = 0;            //set to zero just in case

            quackologist = new Quackologist();
            flockOfDucks.RegisterObserver(quackologist);

            Console.WriteLine("Duck Simulator: With Observer");
            Console.WriteLine(Simulate(flockOfDucks));

            Console.WriteLine("The ducks quacked " + QuackCounter.QuackCount + " times");
        }
        public void Simulate(AbstractDuckFactory duckFactory)
        {
            IQuackable mallardDuck = duckFactory.CreateMallardDuck();
            IQuackable redheadDuck = duckFactory.CreateRedheadDuck();
            IQuackable duckCall    = duckFactory.CreateDuckCall();
            IQuackable rubberDuck  = duckFactory.CreateRubberDuck();
            IQuackable gooseDuck   = new GooseAdapter(new Goose());

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

            Flock flockOfDucks = new Flock();

            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();

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

            flockOfDucks.Add(flockOfMallards);

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

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

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

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

            Console.WriteLine("The ducks quacked " + QuackCounter.NumberOfQuacks + " times.");
        }
        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");
        }
Exemple #6
0
        public void Simulate(AbstractDuckFactory duckFactory, AbstractGooseAdapterFactory gooseAdapterFactory)
        {
            var redheadDuck = duckFactory.CreateRedheadDuck();
            var duckCall    = duckFactory.CreateDuckCall();
            var rubberDuck  = duckFactory.CreateRubberDuck();
            var goose       = gooseAdapterFactory.CreateGooseAdapter();

            var ducksFlock = new DuckFlock();

            ducksFlock.Add(redheadDuck);
            ducksFlock.Add(duckCall);
            ducksFlock.Add(rubberDuck);
            ducksFlock.Add(goose);

            var mallardDuck1      = duckFactory.CreateMallardDuck();
            var mallardDuck2      = duckFactory.CreateMallardDuck();
            var mallardDuck3      = duckFactory.CreateMallardDuck();
            var mallardDuck4      = duckFactory.CreateMallardDuck();
            var mallardDucksFlock = new DuckFlock();

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

            ducksFlock.Add(mallardDucksFlock);

            Console.WriteLine("\n--- Duck simulator ---\n");

//            Console.WriteLine("\n---- Duck simulator: Whole Flock simulation\n");
//            Simulate(ducksFlock);
//
//            Console.WriteLine("\n---- Duck simulator: Whole Flock simulation\n");
//            Simulate(mallardDucksFlock);

//            Console.WriteLine($"The ducks quacked {QuackCounterDecorator.AllQuacksCount} times");

            Console.WriteLine("\n---- Duck simulator: with observer\n");
            var quackologist = new Quackologist();

            ducksFlock.RegisterObserver(quackologist);
            Simulate(ducksFlock);

            Console.WriteLine($"The ducks quacked {QuackCounterDecorator.AllQuacksCount} times");
        }
Exemple #7
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");
        }
Exemple #8
0
        public void Simulate()
        {
            IQuackableObservable mallardDuck = factory.CreateMallardDuck();
            IQuackableObservable redheadDuck = factory.CreateRubberDuck();
            IQuackableObservable duckCall    = factory.CreateDuckCall();
            IQuackableObservable rubberDuck  = factory.CreateRubberDuck();
            IQuackableObservable gooseDuck   = new GooseAdapter(new Goose());


            Flock flockOfDucks = new Flock();

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

            Quackologist q = new Quackologist();

            flockOfDucks.RegisterObserver(q);
            mallardDuck.RegisterObserver(q);



            Console.WriteLine("-----Whole Ducks quack-----\n");

            Simulate(flockOfDucks);



            Console.WriteLine("--------Duck Simulator----------");

            Simulate(mallardDuck);
            Simulate(redheadDuck);
            Simulate(duckCall);
            Simulate(rubberDuck);
            Simulate(gooseDuck);

            Console.WriteLine("Quacks Count:" + QuackCounter.quackCount);
        }
        private void Simulate(AbstractDuckFactory duckFactory)
        {
            IQuackable redheadDuck = duckFactory.CreateRedheadDuck();
            IQuackable duckCall    = duckFactory.CreateDuckCall();
            IQuackable rubberDuck  = duckFactory.CreateRubberDuck();
            IQuackable gooseDuck   = new GooseAdapter(new Goose());

            Flock flockOfDucks = new Flock();

            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();

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

            flockOfDucks.Add(flockOfMallards);

            Console.WriteLine($"{System.Environment.NewLine}Duck Simulator: With Observer");

            Quackologist quackologist = new Quackologist();

            flockOfDucks.RegisterObserver(quackologist);

            Simulate(flockOfDucks);

            Console.WriteLine($"{System.Environment.NewLine}The ducks quacked {QuackCounter.NumberOfQuacks} times");
        }