这段代码违反了开放和封闭原则,随着每年增加更多案例,有什么方法可以重构此代码。
public ISampleDataEnhancer GetEnhancer(Dictionary<string, object> data)
{
var typeEnhancer = ((Data.BaseSampleData)data["DataObject"]).TypeOfEnhancer;
switch (typeEnhancer)
{
case "V2018.SampleA":
{
return new ADataEnhancer2018();
}
case "V2018.SampleB":
{
return new BDataEnhancer2018();
}
case "V2019.SampleA":
{
return new ADataEnhancer2019();
}
case "V2019.SampleB":
{
return new GRDataEnhancer2019();
}
default:
return null;
}
}