0

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);
  }
}
jaco0646
  • 15,303
  • 7
  • 59
  • 83
juckky
  • 493
  • 3
  • 13

1 Answers1

3

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.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82