我有一点设计问题。我创建了一个费率计算器,如下所示:
Public Interface ICalculator
Property PaymentTerm As Double
Function Calculate() As CommissionValues
ReadOnly Property CalculationRule As CalculationRuleEnum
End Interface
Public Interface IFlexibleRateCalculator
Inherits ICalculator
Property TransferRate As Decimal
End Interface
Public Interface IFixedRateCalculator
Inherits ICalculator
Property ContractRate As Decimal
End Interface
Public Interface IRateSettingBase
Property RateType As RateTypeEnum
ReadOnly Property Calculator As ICalculator
End Interface
Public MustInherit Class RateSetting
Implements IRateSettingBase
Public MustOverride ReadOnly Property Calculator() As ICalculator Implements IRateSettingBase.Calculator
我可以做这样的事情:
dim ratevalues as RateValues = RateSetting().Calculator.Calculate()
很简单。问题是每种类型的计算器都有自己的一组属性,需要设置这些属性才能使其Calculate() 方法正常工作。所以我最终不得不执行如下
FlexibleRateCalculator
Implements IFlexibleRateCalculator
Private mRequestedRate As Decimal
Public Function Calculate() As RateValues Implements ICalculator.Calculate
FixedRateCalculator
Implements IFixedRateCalculator
Private mTransferRate As Decimal
Public Function Calculate() As RateValues Implements ICalculator.Calculate
使用泛型和抽象类创建工厂模式以动态生成特定类型的计算器的最佳方法是什么?
我需要一个非常通用的解决方案,因为将使用计算逻辑所需的自己的参数添加和修改许多计算速率。我希望能够快速做到这一点,并可能通过 db 控制这些速率计算。欢迎使用 C# 或 VB.Net 的 FYI 答案:) 提前致谢!