In my factory method, I use the Switch statement to create specific objects. This leads to a very high cyclic complexity. Here is a sample code:
private static UnitDescriptor createUnitDescriptor(string code) { switch (code) { case UnitCode.DEG_C: return new UnitDescriptorDegC(); case UnitCode.DEG_F: return new UnitDescriptorDegF(); : : default: throw new SystemException(string.format("unknown code: {o}", code); } }
How can I reorganize this to reduce cyclic complexity? If I use reflection to create objects or something else to create objects, is this better than the method above?
source share