OOP、数据绑定和用户选择
基本问题是使用数据绑定可以通过哪些方式允许用户选择子类来完成这项工作?
具体例子。我有一个接受 CalculationMethod (接口)来进行计算的类。 CalculationMethod 有多种实现。 GUI 开发人员只想使用数据绑定来向用户提供选择。
我采取了一些方法。
最简单的方法是创建一个类,该类返回所有实现的列表(CalculationMethod),并向 CalculationMethod 添加 Name 属性以用于显示目的。
对此进行扩展,有时我会创建一个使用反射来执行相同操作的类(查找实现 CalculationMethod 的所有类)。这样我就不必记住添加新的实现,但它在 Web 应用程序(成本昂贵)或不允许反射的环境中可能会很糟糕。
有时我添加了一个枚举,每个枚举代表一个实现。有一个工厂方法接受枚举并返回正确的实现。这样 GUI 开发人员就可以绑定到枚举。如果必须以某种方式保留用户选择,我经常这样做。
以上各有优点和缺点。还有其他和/或更好的方法吗?
下面的例子。 GUI 开发人员会将下拉列表(或其他内容)绑定到 CalculationOptions.Calculations 上,以允许用户进行选择。我不会写其他例子,因为你应该明白这个想法。 (我将使用反射来获取继承 CalculatoinTemplate 的所有类,或者我将有一个代表所有继承类的枚举。)
Public Class CalculationTemplate
Public MustOverride Readonly Property Name() as string
Public MustOverride Sub Calculate()
End Class
Public Class CalculationImp1
Public Overrides Sub Calculate()
End Sub
Public Overrides Readonly Property Name() as String
Get
Return "Imp1"
End Get
End Property
End Class
Public Class CalculationImp2
Public Overrides Sub Calculate()
End Sub
Public Overrides Readonly Property Name() as String
Get
Return "Imp2"
End Get
End Property
End Class
Public Class CalculationOptions
Public Shared Function Calculations() as List(Of CalculationTemplate)
Dim lst as New List(Of CalculationTemplate)
lst.add(new CalculationImp1)
lst.add(new CalculationImp2)
Return lst
End Function
End Class
The basic question is using data binding what are the ways you can allow the user to select the subclass to do the job?
Specific example. I have a class that accepts a CalculationMethod (interface) to do the calculation. There are several implementations of CalculationMethod. The GUI developer wants to only use data binding to present the choices to the user.
I have taken a few approaches.
Easiest is to create a class that returns a list(of CalcuationMethod) for all the implementations and add a Name property to CalculationMethod for display purposes.
Expanding on that I will sometimes create a class that uses reflection to do the same thing (finds all the classes that implement CalculationMethod). This way I don't have to remember to add new implementations but it can be bad in web applications (it is expensive) or environments that don't allow reflection.
At times I have added an enumeration with each enum representing an implementation. There is a factory method that accepts the enumeration and returns the proper implementation. This way the GUI developer can bind to the enumeration. I often do this if the user selection must be persisted in some way.
All the above have advantages and disadvantages. Are there other and/or better ways to do this?
Example below. The GUI developer would bind the dropdown (or whatever) to the CalculationOptions.Calculations allowing the user to select. I won't write the other examples because you should get the idea. (I would use reflection to get all the classes that inherit CalculatoinTemplate or I would have an enum that represents all the classes that inherit.)
Public Class CalculationTemplate
Public MustOverride Readonly Property Name() as string
Public MustOverride Sub Calculate()
End Class
Public Class CalculationImp1
Public Overrides Sub Calculate()
End Sub
Public Overrides Readonly Property Name() as String
Get
Return "Imp1"
End Get
End Property
End Class
Public Class CalculationImp2
Public Overrides Sub Calculate()
End Sub
Public Overrides Readonly Property Name() as String
Get
Return "Imp2"
End Get
End Property
End Class
Public Class CalculationOptions
Public Shared Function Calculations() as List(Of CalculationTemplate)
Dim lst as New List(Of CalculationTemplate)
lst.add(new CalculationImp1)
lst.add(new CalculationImp2)
Return lst
End Function
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 CalculationOptions 类中添加一个名为 AddCalculationOption() 的新函数,该函数将计算选项添加到列表中。
使用当前对象并从 name() 返回值作为参数,在 CalculationTemplate 的构造函数中调用 AddCalculationOption 函数。
因此,当创建一个对象时,它会自动添加到列表中。
您可以使用名称来映射名称和对象。工厂可以使用它来返回适当的对象。
我希望我正确理解了你的问题。
Add a new function named AddCalculationOption() in CalculationOptions class, which adds calculation options to list.
Call AddCalculationOption function in constructor of CalculationTemplate with current Object and return value from name() as arguments.
So when an object is created it is automatically added to the list.
You can use the name to have map for name and object. which can be used by factory to return appropriate object.
I hope I understood your question properly.