Cognitive complexity in Factory class

1

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.

    
asked by Dani 08.10.2018 в 14:31
source

1 answer

6

Since any instance must be able to be created in the same way, I assume that all implementations of the interface have a similar constructor (in your example without parameters).

Assuming this, I imagine how you use it as follows:

IMiImplementacion f= ImplementadorFactory.dameImplementacion("Implementacion1");

So my first idea was to simplify your factory's method to something like the following:

public static IMiImplementacion dameImplementacion(String nombreImpl) {
   Class c= Class.forName(nombreImpl);
   return c.newInstance();
}

But then I think there really is no difference with doing:

IMiImplementacion f= ImplementadorFactory.dameImplementacion(Implementacion1.class);

and have the method:

public static IMiImplementacion dameImplementacion(Class implementacion) {
   return implementacion.newInstance();
}

But again, I do not see any contribution from a classic

 IMiImplementacion f= new Implementacion1();

do not do it anymore.

In general, the Factory design should provide some type of added value to the creation of objects (the construction is complex, we want to have singletons, we want to have code injection ...) but in this case would simply create the necessary object in each case, directly.

Assuming that the creation of the objects is something more complex than the one you expose, you could replace the if with a more efficient switch , which is usually more readable when the number of options grows:

public static IMiImplementacion dameImplementacion(String nombreImplentacion) {
    switch(nombreImplementacion) {
      case "nombreImplentacion1":
          return new MiImplementacion1();
      case "nombreImplentacion2":
          return new MiImplementacion2();
      case "nombreImplentacion3":
          return new MiImplementacion3();
      default:
          return null; //o lo que creas conveniente
    }
}
    
answered by 08.10.2018 в 16:20