不幸的是,在每种情况下,Child Table的相关行包含大量不同的数据。因为这是唯一的争议点,我将FetchChildData方法提取到一个名为DetailFinder的单独类中。因此,我的代码看起来像这样:
DetailFinder DetailHandler;
if (ReportType == "Planning")
DetailHandler = new PlanningFinder();
else if (ReportType == "Operations")
DetailHandler = new OperationsFinder();
else if (ReportType == "Maintenance")
DetailHandler = new MaintenanceFinder();
DataTable ChildTable = DetailHandler.FetchChildData(Master);
PlanningFinder、OperationsFinder和MaintenanceFinder都是DetailFinder的子类。
我刚被要求为另一个业务领域添加支持,我不想继续这种if
块趋势。我更喜欢有一个像这样的解析方法:
DetailFinder DetailHandler = DetailFinder.Parse(ReportType);
然而,我不知道如何让 DetailFinder
知道哪个子类处理每个字符串,甚至不知道有哪些子类,而不仅仅是将 if 代码块转移到 Parse
方法中。是否有一种方法可以让子类向抽象的 DetailFinder
注册自己?