Programs:
./build/bin/ap(everything in one class),./build/bin/mvc_ap,./build/bin/mvvm_ap. They are built only when gtkmm-4.0 is installed (CPPLAB_BUILD_GUI).
-
MVVM (Model - View - ViewModel) is an architecture pattern that separates the user interface (
View) from the business logic and data (Model) through an intermediary component called theViewModel -
Components:
- Model: responsible for managing and abstracting data sources (databases, APIs, ..).
ModelandViewModelwork together to get and save the data. - View: displays data provided by the
ViewModeland informs theViewModelabout user actions. This layer observes theViewModeland does not contain application logic. - ViewModel: exposes data and state that are relevant to the
View, and transforms data from Model into a suitable format. Moreover, it serves as a link between theModeland theView. - Binder (Data Binding): connects the View and the ViewModel, automatically synchronizing data between them. This mechanism allows the View to update when the
ViewModelchanges, and vice versa.
- Model: responsible for managing and abstracting data sources (databases, APIs, ..).
-
Workflow:
- User interacts with the
View(e.g., clicks a button). - The
Viewnotifies theViewModel. - The
ViewModelprocesses the input, applies logic, and may request data from theModel. - The
Modelfetches or updates the data (e.g., from an API or database). - The
Modelsends data back to theViewModel. - The
ViewModelupdates the observable data, which automatically updates theViewthroughdata binding or observers.
- User interacts with the
User
|
v
View <-> ViewModel <-> Model # view automatically update
(data binding)-
MVC (Model - View - Controller) is an architectural pattern that separates the user interface (
View) from the application logic and data (Model) using an intermediary component called theController. -
Components:
- Model: is responsible for managing and abstracting data sources (databases, APIs, etc.). The
Modelhandles data retrieval, storage, and business logic. - View: displays the data provided by the
Modeland represents the user interface. TheViewis responsible only for presentation anddoes not contain business logic. - Controller: acts as an intermediary between the
Viewand theModel. It receives user input from theView, processes it, and interacts with theModelto update or retrieve data. TheControllerthen determines whichViewshould display the result.
- Model: is responsible for managing and abstracting data sources (databases, APIs, etc.). The
-
Workflow:
- User interacts with the
View(e.g., clicks a button). - The
Viewsends the user input to theController. - The
Controllerprocesses the input, performs business logic, and may update theModel. - The
Modelupdates its data (e.g., saves to a database or gets data from an API). - The
Controllerthen updates theViewbased on the newModeldata.
- User interacts with the
User
|
v
View -> Controller -> Model
|
v
View # view update manually| Aspect | MVC | MVVM |
|---|---|---|
| Complexity | Lower — Controller is a thin pass-through | Slightly higher — ViewModel adds an extra layer |
| Coupling | Views know both Controller and Model (e.g., for initial data) | Views know only the ViewModel |
| Testability | Controller is testable, but Views are still tied to Model for reads | ViewModel is fully testable without GTK; Views are pure UI |
| Scalability | Adding fields requires updating Model, Controller, and all Views | Adding fields requires updating Model and ViewModel; Views update bindings only |
| Observer wiring | Manual — Container wires each View to the Model | Self-contained — Views register via ViewModel; container stays clean |
| UI logic leakage | Risk - Views may call model_->getData() directly |
Eliminated - Views use viewModel_->getCurrentText() only |