I'm learning about design patterns from GOF book.
There is one thing about Abstract Factory pattern implementation that makes me mingle. I'll start with code:
Abstract Factory:
public interface AnimalsFactory {
Animal getWaterAnimal();
Animal getLandAnimal();
Animal getFlyingAnimal();
}
Concrete Factory:
public class SafariAnimalsFactory implements AnimalsFactory {
@Override
public Hippo getWaterAnimal() {
return new Hippo();
}
@Override
public Giraffe getLandAnimal() {
return new Giraffe();
}
@Override
public Lion getFlyingAnimal() {
return new Lion();
}
}
I used covariance of types so factory methods return concrete products instead of abstract products (Animal).
I like it but doesn't this violate rule of thumb about products being encapsulated from the client? Or maybe I'am overthinking this.
If this is too much opinion based question I'm willing to delete it.
Aucun commentaire:
Enregistrer un commentaire