public class DuckFactory implements AbstractDuckFactory {
@Override
public Quackable createMallardDuck() {
return new MallardDuck();
}
@Override
public Quackable createRedheadDuck() {
return new RedheadDuck();
}
@Override
public Quackable createDuckCall() {
return new DuckCall();
}
@Override
public Quackable createRubberDuck() {
return new RubberDuck();
}
}
public class DuckFactory implements AbstractDuckFactory {
- This declares a public class named
DuckFactory
.
- It implements the
AbstractDuckFactory
interface, indicating that instances of this class can create different types of ducks.
@Override public Quackable createMallardDuck() { return new MallardDuck(); }
- This method is part of the
AbstractDuckFactory
interface implementation.
- It creates and returns a new instance of
MallardDuck
, representing the creation of a Mallard duck.
@Override public Quackable createRedheadDuck() { return new RedheadDuck(); }
- This method is part of the
AbstractDuckFactory
interface implementation.
- It creates and returns a new instance of
RedheadDuck
, representing the creation of a Redhead duck.
@Override public Quackable createDuckCall() { return new DuckCall(); }
- This method is part of the
AbstractDuckFactory
interface implementation.
- It creates and returns a new instance of
DuckCall
, representing the creation of a Duck Call.
@Override public Quackable createRubberDuck() { return new RubberDuck(); }
- This method is part of the
AbstractDuckFactory
interface implementation.
- It creates and returns a new instance of
RubberDuck
, representing the creation of a Rubber duck.
}
- Closes the
DuckFactory
class.
- Overall Explanation:
- The
DuckFactory
class is an implementation of the AbstractDuckFactory
interface.
- It provides concrete implementations for creating different types of ducks (Mallard, Redhead, Duck Call, Rubber Duck).
- Each creation method returns a new instance of the respective duck type.
- This class is used to create ducks in the
DuckSimulator
and can be easily replaced with another factory that adheres to the same interface, allowing for flexibility in creating different types of ducks.