I have a question about how to correctly create a factory class without increasing the cognitive complexity.
The class consists of a static method that returns an implementation based on a name.
Being a large application, there are many implementations so validating which one has to be used requires many conditionals. And therefore, the cognitive complexity adds one for each if
.
The class would be something like this:
public class ImplementadorFactory {
private ImplementadorFactory () {
}
public static IMiImplementacion dameImplementacion(String nombreImplentacion) {
if ("nombreImplentacion".equals(nombreImplentacion)) {
return new MiImplementacion1();
}
if ("nombreImplentacion2".equals(nombreImplentacion)) {
return new MiImplementacion2();
}
if ("nombreImplentacion3".equals(nombreImplentacion)) {
return new MiImplementacion3();
}
/*...Doscientas mil implementaciones más.....*/
}
}
Is there any other better way to use this pattern? Is it possible that I did not choose the right pattern for my need?
The utility that I want to give to this is to be able to make custom forms, which, some of them are simple and always do the same, obtain data, save .. and others that carry another series of specific functions. The latter are the ones that would work with these particular implementations. The name of implementation would be obtained for example from a database, when a developer wants to create a new form, could put its name and perform its particular logic.