Reputation: 503
Working on a project where the flavour of the month seems to be using switch statements and enums to decide what concrete class instance to create.
Is there an alternative design pattern that could be considered for object creation, taking into account that they all implement the same interface, but require different object parameters when creating the instance?
For example, the current code is.
SomeInterface concreteInstance;
Switch() {
case A :
{
concreteInstance = new ConcreteAInstance(param1, param2);
}
case b :
{
concreteInstance = new ConcreteBInstance(param1, param2, param3);
}
case c :
{
concreteInstance = new ConcreteCInstance(param1);
}
}
Upvotes: 0
Views: 771
Reputation: 36143
Take a look at the Abstract Factory pattern:
https://en.wikipedia.org/wiki/Abstract_factory_pattern
Btw. there is no "factory pattern". The gang of four defines two flavors: Abstract Factory and Factory Method.
Upvotes: 3