public void Run() { CountingDuckFactory duckFactory = new CountingDuckFactory(); IQuackable redHeadDuck = duckFactory.CreateRedHeadDuck(); IQuackable duckCall = duckFactory.CreateDuckCall(); IQuackable rubberDuck = duckFactory.CreateRubberDuck(); IQuackable gooseAdapter = new GooseAdapter(new Goose()); Flock flockOfMallards = new Flock(); flockOfMallards.Add(duckFactory.CreateMallardDuck()); flockOfMallards.Add(duckFactory.CreateMallardDuck()); flockOfMallards.Add(duckFactory.CreateMallardDuck()); flockOfMallards.Add(duckFactory.CreateMallardDuck()); Flock flockOfDucks = new Flock(); flockOfDucks.Add(redHeadDuck); flockOfDucks.Add(duckCall); flockOfDucks.Add(rubberDuck); flockOfDucks.Add(gooseAdapter); flockOfDucks.Add(flockOfMallards); flockOfDucks.RegisterObserver(new Quackologist()); Console.WriteLine("--- Duck Simulator ---"); Simulate(flockOfDucks); Console.WriteLine($"{QuackCounterDecorator.NumberOfQuacks} quacks recorded"); }
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); 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: With Observer"); Quackologist quackologist = new Quackologist(); flockOfDucks.RegisterObserver(quackologist); Simulate(flockOfDucks); Console.WriteLine($"\nThe ducks quacked {QuackCounter.NumberOfQuacks} times"); }