MVC 方法与 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6929957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC approach with C++
提问by Stefano Mtangoo
I have been learning PHP MVC pattern and it is pretty cool. have almost finished app and I can see how mess you can make a code without good design. Now can MCV be applied to C++ apps? Where does Plugin manager/Plugins go if that is even possible?In model or controller? Thanks!
我一直在学习 PHP MVC 模式,它非常酷。几乎完成了应用程序,我可以看到如果没有良好的设计,您可以制作出多么混乱的代码。现在可以将 MCV 应用于 C++ 应用程序吗?如果可能的话,插件管理器/插件会去哪里?在模型还是控制器中?谢谢!
EDIT:
编辑:
I mean C++ with GUI toolkit like QT/Wxwidgets/GTK+ Also Please help me on how to implement in C++. I have learned how to do it in PHP but as you know the two languages are somehow different!
我的意思是 C++ 和 GUI 工具包,如 QT/Wxwidgets/GTK+ 也请帮助我如何在 C++ 中实现。我已经学会了如何在 PHP 中做到这一点,但正如您所知,这两种语言在某种程度上是不同的!
EDIT2
编辑2
回答by lurscher
how do you actually implement it in C++
你如何在 C++ 中实际实现它
make classes in charge of rendering know nothing about application details. Call them SomethingView classes to make this point clear
make your domain objects not know anything about visualization or user interaction. You don't need to call them Model, but you could
create a set of classes in charge of running the role of Controllers: wire somehow dependencies to view and model classes via dependency injection if possible. example: CppInject. In any case, controller classes can know both about model and view classes, so the important part is this: all the coupling between view and model objects is isolated to the controllers.
Also, this implies, that all imperative-style programming should be confined to the controller classes as well: view and model should be declarative-style. That means, they should offer services related to its role, but avoid direct interaction with other objects as side-effects
It is not true you need to implement communication between controllers and the other components with event-style system, although such system is definitely helpful, but certainly not required
surprise!the above applies to any language or framework, except of course languages that somehow already force MVC down your throat from the start, i.e: ruby on rails
让负责渲染的类对应用程序的细节一无所知。将它们称为SomethingView 类以明确这一点
让你的领域对象对可视化或用户交互一无所知。你不需要称他们为模型,但你可以
创建一组负责运行控制器角色的类:如果可能的话,通过依赖注入以某种方式将依赖连接到视图和模型类。示例:CppInject。在任何情况下,控制器类都可以知道模型和视图类,所以重要的部分是:视图和模型对象之间的所有耦合都与控制器隔离。
此外,这意味着,所有命令式编程也应该局限于控制器类:视图和模型应该是声明式的。这意味着,它们应该提供与其角色相关的服务,但避免与其他对象直接交互作为副作用
您不需要使用事件样式系统来实现控制器和其他组件之间的通信,虽然这样的系统肯定有帮助,但肯定不是必需的
惊喜!以上适用于任何语言或框架,当然除了那些从一开始就以某种方式迫使 MVC 扼杀你的语言,即:ruby on rails
回答by Alok Save
MVC is a design pattern not a language specific construct, So yes you can apply it to C++ app as well.
MVC 是一种设计模式,而不是特定于语言的构造,所以是的,您也可以将其应用于 C++ 应用程序。
MVC can and should be applied in any language so your User Interface is loosely coupled with the backend & either can be changed with minimum impact on each other.
MVC 可以而且应该以任何语言应用,因此您的用户界面与后端松散耦合,并且可以在对彼此影响最小的情况下进行更改。
The MVC pattern provides a clean separation of objects into:
MVC 模式将对象清晰地分离为:
- Modelsfor maintaining data,
- Viewsfor displaying all or a portion of the data, and
- Controllersfor handling events that affect the model or view(s).
- 用于维护数据的模型,
- 用于显示全部或部分数据的视图,以及
- 用于处理影响模型或视图的事件的控制器。
回答by Armen Tsirunyan
Yes, MVC can be applied in C++. For example, the MFC framework uses Document/View architecture which is essentially an MVC.
是的,MVC 可以应用于 C++。例如,MFC 框架使用本质上是 MVC 的文档/视图架构。
A design pattern isn't a library or class. It's a pattern. So you don't have a generic MVC library for C++.
设计模式不是库或类。这是一种模式。因此,您没有用于 C++ 的通用 MVC 库。
回答by MaK
Use Tree frogs Framework. TreeFrog Framework is a high-speed and full-stack C++ framework for developing Web applications.
使用树蛙框架。TreeFrog 框架是用于开发 Web 应用程序的高速全栈 C++ 框架。
回答by ascanio
MVCis an architectural design pattern (i.e. a wayof building software) commonly associated with web applications, but it is applicable in general to any software project in any language. You have to make a little abstraction effort on your project, and identify which piece of software belongs to each part (i.e. a GUI is probably part of View, etc.).
MVC是一种架构设计模式(即一种构建软件的方式),通常与 Web 应用程序相关联,但它通常适用于任何语言的任何软件项目。您必须对您的项目进行一些抽象工作,并确定每个部分属于哪个软件(即 GUI 可能是视图的一部分,等等)。
Note that this type of pattern is mainly aimed to separate developement, so that any part of the project can be developed regardless of the others. This can be annoying for a small standalone application, but useful and rewarding on bigger projects.
请注意,这种类型的模式主要是为了分开开发,以便项目的任何部分都可以开发而不管其他部分。这对于小型的独立应用程序来说可能很烦人,但对大型项目来说却是有用且有益的。
回答by Du Bois Eloi
Personally, I use boost state machines for the logical and boost signals to connect things together.
就个人而言,我将升压状态机用于逻辑和升压信号以将事物连接在一起。
I wrote a little example that you can analyze here: https://github.com/edubois/mvp-player
我写了一个小例子,你可以在这里分析:https: //github.com/edubois/mvp-player