在我的工厂方法中,我使用Switch语句来创建具体对象.这导致非常高的圈复杂度.这是一个示例代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能重构这个以减少圈复杂度?如果我使用反射创建对象或其他东西来构建对象,它比上面的方法更好吗?