001package headfirst.combining.factory;
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                Quackable mallardDuck = duckFactory.createMallardDuck();
013                Quackable redheadDuck = duckFactory.createRedheadDuck();
014                Quackable duckCall = duckFactory.createDuckCall();
015                Quackable rubberDuck = duckFactory.createRubberDuck();
016                Quackable gooseDuck = new GooseAdapter(new Goose());
017
018                System.out.println("\nDuck Simulator: With Abstract Factory");
019
020                simulate(mallardDuck);
021                simulate(redheadDuck);
022                simulate(duckCall);
023                simulate(rubberDuck);
024                simulate(gooseDuck);
025
026                System.out.println("The ducks quacked " +
027                                QuackCounter.getQuacks() +
028                                " times");
029        }
030
031        void simulate(Quackable duck) {
032                duck.quack();
033        }
034}