001package headfirst.combining.observer;
002
003public class DuckSimulator {
004        public static void main(String[] args) {
005                DuckSimulator simulator = new DuckSimulator();
006                AbstractDuckFactory duckFactory = new CountingDuckFactory();
007
008                simulator.simulate(duckFactory);
009        }
010
011        void simulate(AbstractDuckFactory duckFactory) {
012
013                Quackable redheadDuck = duckFactory.createRedheadDuck();
014                Quackable duckCall = duckFactory.createDuckCall();
015                Quackable rubberDuck = duckFactory.createRubberDuck();
016                Quackable gooseDuck = new GooseAdapter(new Goose());
017
018                Flock flockOfDucks = new Flock();
019
020                flockOfDucks.add(redheadDuck);
021                flockOfDucks.add(duckCall);
022                flockOfDucks.add(rubberDuck);
023                flockOfDucks.add(gooseDuck);
024
025                Flock flockOfMallards = new Flock();
026
027                Quackable mallardOne = duckFactory.createMallardDuck();
028                Quackable mallardTwo = duckFactory.createMallardDuck();
029                Quackable mallardThree = duckFactory.createMallardDuck();
030                Quackable mallardFour = duckFactory.createMallardDuck();
031
032                flockOfMallards.add(mallardOne);
033                flockOfMallards.add(mallardTwo);
034                flockOfMallards.add(mallardThree);
035                flockOfMallards.add(mallardFour);
036
037                flockOfDucks.add(flockOfMallards);
038
039                System.out.println("\nDuck Simulator: With Observer");
040
041                Quackologist quackologist = new Quackologist();
042                flockOfDucks.registerObserver(quackologist);
043
044                simulate(flockOfDucks);
045
046                System.out.println("\nThe ducks quacked " +
047                                QuackCounter.getQuacks() +
048                                " times");
049        }
050
051        void simulate(Quackable duck) {
052                duck.quack();
053        }
054}