MVC approach with C++

2019-01-21 20:17发布

问题:

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!

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!

EDIT2

http://forums.wxwidgets.org/viewtopic.php?f=1&t=30983

回答1:

how do you actually implement it in 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



回答2:

MVC is a design pattern not a language specific construct, So yes you can apply it to C++ app as well.

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.

The MVC pattern provides a clean separation of objects into:

  • Models for maintaining data,
  • Views for displaying all or a portion of the data, and
  • Controllers for handling events that affect the model or view(s).


回答3:

Yes, MVC can be applied in C++. For example, the MFC framework uses Document/View architecture which is essentially an 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++.



回答4:

Use Tree frogs Framework. TreeFrog Framework is a high-speed and full-stack C++ framework for developing Web applications.



回答5:

MVC is an architectural design pattern (i.e. a way of 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.).

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.



回答6:

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