简体   繁体   English

对返回相似数据的多个对象进行适当的设计

[英]Appropriate design for multiple objects that return similar data

I'm working on the following scenario: 我正在处理以下情况:

  • A View has several tabs. 视图有几个选项卡。 Each tab is represented by a separate class and contains a data grid. 每个选项卡由一个单独的类表示,并包含一个数据网格。

  • The tabs can each return the set of items that the user has selected. 每个选项卡都可以返回用户选择的项目集。 These items are very similar, but each one has one or two additional properties that are specific to it. 这些项目非常相似,但是每个项目都有一个或两个特定于它的附加属性。

  • The View Presenter has a method called SendItems which should get the items from the current active tab and call the appropriate backend service method (one method per different collection of items). View Presenter有一个名为SendItems的方法,该方法应从当前活动选项卡中获取项目并调用适当的后端服务方法(每个不同的项目集合一个方法)。

What is the appropriate design here? 这里合适的设计是什么? I have thought of two ways so far: 到目前为止,我已经想到了两种方法:

  • Since the Items are very similar, I could theoretically group all the properties into 1 Item class and then I can abstract the whole thing out by having a method called GetItems() which would return the collection. 由于Items非常相似,因此从理论上讲,我可以将所有属性分组到1 Item类中,然后可以通过使用名为GetItems()的方法将整个内容抽象出来,该方法将返回集合。 I can then use a delegate dictionary on the tab type to call the right back-end method. 然后,我可以在选项卡类型上使用委托字典来调用正确的后端方法。

  • I can have all the tabs derive from a base tab, store the list of tabs in the Presenter and hold the Current tab. 我可以让所有标签都来自基本标签,将标签列表存储在Presenter中并按住Current标签。 On SendItems, make several conditions (one per type (if CurrentTab is TabA => MethodA(), etc..)) and then downcast to retrieve the correct data. 在SendItems上,设置几个条件(每种类型一个(如果CurrentTab is TabA => MethodA()等)。)然后向下转换以检索正确的数据。 Finally, call the appropriate back-end method. 最后,调用适当的后端方法。

I don't find either solution appealing (group separate properties into one class / downcasting) and was hoping there is a standard way of solving this type of problem. 我认为这两种解决方案都没有吸引力(将单独的属性分组为一个类/向下转换),并希望有一种解决此类问题的标准方法。

I believe in your case, I would go mostly with option 2. 我相信在您的情况下,我主要会选择选项2。

  1. Create a parent tab class and make your other tabs derive from it. 创建一个父选项卡类,并使其他选项卡派生自它。
  2. Create a parent class for all of your items which has all of the shared properties in it. 为所有具有所有共享属性的项目创建一个父类。 Specific item types for each tab can derive from the parent class to define their extra properties 每个选项卡的特定项类型可以从父类派生,以定义其额外属性
  3. In the parent tab, keep a list of the selected items. 在父选项卡中,保留所选项目的列表。 The list should be a List. 该列表应该是一个列表。 Child classes can get the items in this collection and rely on runtime casting to get more specific types. 子类可以获取此集合中的项目,并依靠运行时强制转换来获取更多特定类型。 In your case, the cast will basically be a noop (depending on how you do it) so there aren't really any performance issues there. 在您的情况下,强制转换基本上不会发生(取决于您的操作方式),因此实际上并没有任何性能问题。
  4. On the parent tab, make the SendItems method abstract so each child tab has to implement it themselves 在父选项卡上,使SendItems方法成为抽象,以便每个子选项卡都必须自己实现

This is close to your second option above, except for taking advantage of a little more polymorphism magic and avoiding your conditions in SendItems - if you have conditions like that, just offload the logic to the child classes instead. 这与上面的第二个选项非常接近,除了利用更多的多态性魔术并避免在SendItems中使用条件-如果您具有这样的条件,只需将逻辑卸载到子类中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM