001package headfirst.combining.composite; 002 003public class DuckSimulator { 004 005 public static void main(String[] args) { 006 DuckSimulator simulator = new DuckSimulator(); 007 AbstractDuckFactory duckFactory = new CountingDuckFactory(); 008 009 simulator.simulate(duckFactory); 010 } 011 012 void simulate(AbstractDuckFactory duckFactory) { 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 Composite - Flocks"); 019 020 Flock flockOfDucks = new Flock(); 021 022 flockOfDucks.add(redheadDuck); 023 flockOfDucks.add(duckCall); 024 flockOfDucks.add(rubberDuck); 025 flockOfDucks.add(gooseDuck); 026 027 Flock flockOfMallards = new Flock(); 028 029 Quackable mallardOne = duckFactory.createMallardDuck(); 030 Quackable mallardTwo = duckFactory.createMallardDuck(); 031 Quackable mallardThree = duckFactory.createMallardDuck(); 032 Quackable mallardFour = duckFactory.createMallardDuck(); 033 034 flockOfMallards.add(mallardOne); 035 flockOfMallards.add(mallardTwo); 036 flockOfMallards.add(mallardThree); 037 flockOfMallards.add(mallardFour); 038 039 flockOfDucks.add(flockOfMallards); 040 041 System.out.println("\nDuck Simulator: Whole Flock Simulation"); 042 simulate(flockOfDucks); 043 044 System.out.println("\nDuck Simulator: Mallard Flock Simulation"); 045 simulate(flockOfMallards); 046 047 System.out.println("\nThe ducks quacked " + 048 QuackCounter.getQuacks() + 049 " times"); 050 } 051 052 void simulate(Quackable duck) { 053 duck.quack(); 054 } 055}