public void simulate()
        {
            Quackable mallard = new QuackCounter(new MallardDuck());
            Quackable redhead = new QuackCounter(new RedheadDuck());
            Quackable call    = new QuackCounter(new DuckCall());
            Quackable rubber  = new QuackCounter(new RubberDuck());
            Quackable goose   = new GooseAdapter(new Goose());

            simulate(mallard);
            simulate(redhead);
            simulate(call);
            simulate(rubber);
            simulate(goose);

            Console.WriteLine("The ducks quacked " + QuackCounter.getQuacks() + " times");
        }
Example #2
0
        public void SimulateUsingFactory(AbstractDuckFactory duckFactory)
        {
            var mallardDuck = duckFactory.CreateMallardDuck;
            var redheadDuck = duckFactory.CreateRedheadDuck;
            var duckCall    = duckFactory.CreateDuckCall;
            var rubberDuck  = duckFactory.CreateRubberDuck;
            var gooseDuck   = new GooseAdapter(new Goose());

            Console.WriteLine("\nDuck Simulator: With Abstract Factory");
            Simulate(mallardDuck);
            Simulate(redheadDuck);
            Simulate(duckCall);
            Simulate(rubberDuck);
            Simulate(gooseDuck);

            Console.WriteLine($"The ducks quacked {QuackCounter.GetQuacksCount()} times.");
        }
Example #3
0
        public void SimulateWithCounter()
        {
            var mallardDuck = new QuackCounter(new MallardDuck());
            var redheadDuck = new QuackCounter(new RedheadDuck());
            var duckCall    = new QuackCounter(new DuckCall());
            var rubberDuck  = new QuackCounter(new RubberDuck());
            var gooseDuck   = new GooseAdapter(new Goose());

            Console.WriteLine("\nDuck Simulator: With Decorator");
            Simulate(mallardDuck);
            Simulate(redheadDuck);
            Simulate(duckCall);
            Simulate(rubberDuck);
            Simulate(gooseDuck);

            Console.WriteLine($"The ducks quacked {QuackCounter.GetQuacksCount()} times.");
        }
Example #4
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");
        }
Example #5
0
        static void Simulator(AbstractDuckFactory duckFactory)
        {
            Quackable redheadDuck  = duckFactory.CreateReadheadDuck();
            Quackable rubberDuck   = duckFactory.CreateRubberDuck();
            Quackable gooseDuck    = new GooseAdapter(new Goose());
            Flock     flockOfDucks = new Flock();

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

            QuackObserver observer = new QuackObserver();

            flockOfDucks.RegisterObserver(observer);

            Console.WriteLine("模拟器");
            Simulator(flockOfDucks);
            Console.WriteLine("鸭子叫声:" + QuackCounter.GetQuacks());
        }
        public 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);


            IQuackable mallard1 = duckFactory.createMallardDuck();
            IQuackable mallard2 = duckFactory.createMallardDuck();
            IQuackable mallard3 = duckFactory.createMallardDuck();
            IQuackable mallard4 = duckFactory.createMallardDuck();

            Flock flockOfMallards = new Flock();

            flockOfMallards.add(mallard1);
            flockOfMallards.add(mallard2);
            flockOfMallards.add(mallard3);
            flockOfMallards.add(mallard4);

            flockOfDucks.add(flockOfMallards);

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

            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("\nThe ducks quacked: " + QuackCounter.GetQuacks());
        }
Example #7
0
        public void SimulateFlockWithObserver(AbstractDuckFactory duckFactory)
        {
            var mallardDuck = duckFactory.CreateMallardDuck;
            var redheadDuck = duckFactory.CreateRedheadDuck;
            var duckCall    = duckFactory.CreateDuckCall;
            var rubberDuck  = duckFactory.CreateRubberDuck;
            var gooseDuck   = new GooseAdapter(new Goose());

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

            var flockOfDucks = new Flock.Flock();

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

            var flockOfMallardDucks = new Flock.Flock();
            var mallard1            = duckFactory.CreateMallardDuck;
            var mallard2            = duckFactory.CreateMallardDuck;
            var mallard3            = duckFactory.CreateMallardDuck;
            var mallard4            = duckFactory.CreateMallardDuck;

            flockOfMallardDucks.Add(mallard1);
            flockOfMallardDucks.Add(mallard2);
            flockOfMallardDucks.Add(mallard3);
            flockOfMallardDucks.Add(mallard4);
            flockOfDucks.Add(flockOfMallardDucks);

            var quackologist = new Quackologist();

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

            Console.WriteLine($"The ducks quacked {QuackCounter.GetQuacksCount()} times.");
        }
        public void simulate(AbstractDuckFactory factory)
        {
            Quackable redhead = factory.createRedheadDuck();
            Quackable call    = factory.createDuckCall();
            Quackable rubber  = factory.createRubberDuck();
            Quackable goose   = new GooseAdapter(new Goose());

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

            Flock flockOfDucks = new Flock();

            flockOfDucks.add(redhead);
            flockOfDucks.add(call);
            flockOfDucks.add(rubber);
            flockOfDucks.add(goose);

            Flock flockOfMallards = new Flock();

            Quackable mallard1 = factory.createMallardDuck();
            Quackable mallard2 = factory.createMallardDuck();
            Quackable mallard3 = factory.createMallardDuck();
            Quackable mallard4 = factory.createMallardDuck();

            flockOfMallards.add(mallard1);
            flockOfMallards.add(mallard2);
            flockOfMallards.add(mallard3);
            flockOfMallards.add(mallard4);

            flockOfDucks.add(flockOfMallards);

            Console.WriteLine("Duck Simulator: Whole Flock Simulation");
            simulate(flockOfDucks);
            Console.WriteLine("Duck Simulator: Mallard Flock Simulation");
            simulate(flockOfMallards);

            Console.WriteLine("The ducks quacked " + QuackCounter.getQuacks() + " times");
        }