如何避免GUI控件和域对象之间的并行继承层次结构 [英] How to avoid parallel inheritance hierarchies among GUI controls and domain objects

查看:118
本文介绍了如何避免GUI控件和域对象之间的并行继承层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发Windows窗体应用程序。我正在使用MVP设计模式。在GUI中,用户可以操作打印机对象,并且需要有自定义控件来向用户表示打印机对象。

I am developing a Windows Forms application in C#. I am using the MVP design pattern. In the GUI the user can manipulate printer objects, and there needs to be custom controls that represent the printer objects to the user.

有一个表示打印机的类层次结构。在基础上有一个抽象的Printer类。然后是一个抽象的InkJetPrinter类和一个抽象的LaserPrinter类,每个类都继承自Printer。具体类表示打印机的不同品牌和型号,并从InkJetPrinter或LaserPrinter继承。

There is a class hierarchy that represents the printers. At the base there is an abstract Printer class. Then there is an abstract InkJetPrinter class and an abstract LaserPrinter class, each inheriting from Printer. The concrete classes represent the different makes and models of printers and inherit from either InkJetPrinter or LaserPrinter.

GUI中代表打印机的每个UserControl类都需要具有特性和功能特定于打印机的类型。例如,喷墨打印机可能有一个显示墨水量的显示器,某个型号的激光打印机可能具有可以从控件上的附加按钮访问的特殊功能等。

Each UserControl class in the GUI that represents a printer needs to have features and functionality specific to the type of printer. For example, an ink jet printer might have a display that shows the ink level, a certain model of laser printer might have special features that could be accessed from an additional button on the control, etc.

我没有看到更好的处理方法,而不是让UserControl类的继承层次与Printer类的继承层次结构并行。有没有更好的方法?

I do not see a better way of handling this than to have an inheritance hierachy of UserControl classes parallel to the inheritance hierarchy of Printer classes. Is there a better approach?

推荐答案

我不认为这是一个经典的并行继承层次结构?如果是,那么我认为编写GUI控件类将是必需,以便非GUI打印机类能够完全正常运行。情况并非如此,因为人们可以使用打印机类而不是随附的UserControl类来编写功能齐全的非GUI应用程序。

I don't think this is a classic parallel inheritance hierarchy? If it were, then I think it would be required to write a GUI control class in order for the non-GUI printer classes to be fully functional. That is not the case since one could write a fully functional non-GUI application just using the printer classes and not the accompanying UserControl classes.

这篇关于如何避免GUI控件和域对象之间的并行继承层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆