public class DuckSimulator {
public static void main(String[] args) {
DuckSimulator simulator = new DuckSimulator();
AbstractDuckFactory duckFactory = new CountingDuckFactory();
simulator.simulate(duckFactory);
}
void simulate(AbstractDuckFactory duckFactory) {
Quackable redheadDuck = duckFactory.createRedheadDuck();
Quackable duckCall = duckFactory.createDuckCall();
Quackable rubberDuck = duckFactory.createRubberDuck();
Quackable 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();
Quackable mallardOne = duckFactory.createMallardDuck();
Quackable mallardTwo = duckFactory.createMallardDuck();
flockOfMallards.add(mallardOne);
flockOfMallards.add(mallardTwo);
flockOfDucks.add(flockOfMallards);
System.out.println("\\nDuck Simulator: With Observer");
Quackologist quackologist = new Quackologist();
flockOfDucks.registerObserver(quackologist);
simulate(flockOfDucks);
System.out.println("\\nThe ducks quacked " + QuackCounter.getQuacks() + " times");
}
void simulate(Quackable duck) {
duck.quack();
}
}
public class DuckSimulator {
- This declares a public class named
DuckSimulator.
public static void main(String[] args) { ... }
- This is the entry point of the program. It contains the
main method, where the program execution starts.
DuckSimulator simulator = new DuckSimulator();
- Creates an instance of
DuckSimulator.
AbstractDuckFactory duckFactory = new CountingDuckFactory();
- Creates an instance of
CountingDuckFactory and assigns it to the duckFactory variable, demonstrating the use of the Abstract Factory pattern.
simulator.simulate(duckFactory);
- Calls the
simulate method on the simulator object, passing the duckFactory as a parameter.
void simulate(AbstractDuckFactory duckFactory) { ... }
- This method simulates the behavior of ducks using the provided
duckFactory.
Quackable redheadDuck = duckFactory.createRedheadDuck();
- Creates a
Quackable redhead duck using the duckFactory.
Quackable duckCall = duckFactory.createDuckCall();
- Creates a
Quackable duck call using the duckFactory.
Quackable rubberDuck = duckFactory.createRubberDuck();
- Creates a
Quackable rubber duck using the duckFactory.
Quackable gooseDuck = new GooseAdapter(new Goose());
- Creates a
Quackable goose duck using the GooseAdapter.
Flock flockOfDucks = new Flock();
- Creates a flock of ducks using the
Flock class.
flockOfDucks.add(redheadDuck); ... flockOfDucks.add(gooseDuck);
- Adds different ducks and goose to the flock.
Flock flockOfMallards = new Flock(); ... flockOfDucks.add(flockOfMallards);
- Creates a flock of mallard ducks, adds mallards to it, and then adds the flock of mallards to the main flock.
Quackologist quackologist = new Quackologist();
- Creates an instance of the
Quackologist observer.
flockOfDucks.registerObserver(quackologist);
- Registers the
quackologist observer with the main flock of ducks.
simulate(flockOfDucks);
- Calls the
simulate method on the main flock of ducks.
System.out.println("\\nThe ducks quacked " + QuackCounter.getQuacks() + " times");
- Prints the total number of quacks by accessing the
QuackCounter static method getQuacks().