When I feel the need of having a factory class in my projects, I'm tempted to use this approach:
public class ProductFactory {
public Product makeProduct(Type t){
switch (t) {
case A: return new ProductA(); break;
case B: return new ProductB(); break;
default: throw new UnsupportedTypeException();
}
return null;
}
}
Where the logic for the product selection is encapsulated within the factory object.
With the abstract factory pattern, instead, from what I have learned, the approach is to abstract the factory as well, and instantiate it according to the product selection logic.
public class ProductFactoryFactory {
public ProductFactory getProductFactory(Type t){
switch (t) {
case A: return new ProductFactoryA(); break;
case B: return new ProductFactoryB(); break;
default: throw new UnsupportedTypeException();
}
return null;
}
}
In both cases whenever a new product is added, a switch needs to change somewhere in order to include the newly created product.
My question is if I'm missing something with my approach and what would be the advantages of using the abstract factory instead.