简体   繁体   English

如何在MFC中将GUI与逻辑分开?

[英]How to separate GUI from logic in MFC?

I want to develop a MFC application in VS2010. 我想在VS2010中开发MFC应用程序。 I hope to separate GUI from the logic, so that modifying GUI can become much easier. 我希望将GUI与逻辑分开,以便修改GUI变得容易得多。 But I don't know how to design the classes to implement this function. 但是我不知道如何设计类来实现此功能。 Which design pattern should I use? 我应该使用哪种设计模式? Does MFC have any special way to deal with the problem? MFC是否有任何特殊的方法来解决此问题?

Moreover, I am not familiar with design patterns. 而且,我对设计模式不熟悉。 So I hope someone can give me samples or detailed articles explaining this. 因此,我希望有人可以给我提供样本或详细的文章来对此进行解释。 (Also I prefer a less complicated pattern! ^^) (而且我更喜欢一个不太复杂的模式!^^)

Thank you very much! 非常感谢你!

The MFC already provide such a separation in their SDI/MDI based templates. MFC已经在其基于SDI / MDI的模板中提供了这种分离。 For example, you have a CWinApp instance and a CMainFrame for the program itself. 例如,您有一个CWinApp实例和一个程序本身的CMainFrame。 For each file in MDI apps you have a CDocument and a CView class. 对于MDI应用程序中的每个文件,您都有一个CDocument和一个CView类。

Note: 注意:

  • The MFC don't use the classic MVC separation, they combine the view and controller into a single UI part. MFC不使用经典的MVC分隔,而是将视图和控制器组合到单个UI部件中。
  • The MFC are not strict about this, you can also put a button handler into the application/document, not only into the frame/view. MFC对此并不严格,您还可以在应用程序/文档中放置按钮处理程序,而不仅是在框架/视图中。
  • You don't fully separate the model from the MFC, it is still built on that. 您没有将模型与MFC完全分开,它仍基于MFC建立。 If you want to achieve this separation, you have to do additional work, but then you have a module that you can test completely separately. 如果要实现这种分离,则必须做其他工作,但是您有一个可以完全单独测试的模块。 This allows you to use test-driven development, which is more difficult when embedded into a GUI. 这使您可以使用测试驱动的开发,将其嵌入GUI时会更加困难。

May be it will be helpful http://martinfowler.com/eaaDev/uiArchs.html . 可能会对http://martinfowler.com/eaaDev/uiArchs.html有所帮助。 Also try to find MVC (model/view/controller) pattern. 还尝试找到MVC(模型/视图/控制器)模式。

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

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