The Model-View-Controller (MVC) is one of the classic architectural patterns from the book “Pattern-Oriented Software Architecture, Volume 1“. It addresses interactive applications with a flexible human-machine interface.
The MVC divides the program logic of a user interface into separate components model, view, and controller. The model manages the data and rules of the application. The view represents the data, and the controller interacts with the user.
- User interfaces need to be changed frequently
- Different user interfaces must be supported
- The data model is stable
- The application is divided into the components Model (data model), View (output components), and Controller (input components)
- Multiple output components can use the same data model
- The central component of the pattern
- Contains the data (and the business logic)
- Is independent of the
- Is responsible for the representing of the data and user interaction
- Observes the
Viewis associated with a
Controllerto manipulate the Model
- Manages one or more
- Accepts user interactions and prepares them for the
- Observes the
- Implements the update logic
There are two exciting aspects of the MVC: initialization and user input:
The following steps happen during the initialization of the MVC:
- The model is created, and it initializes its data
- The view is created and observes the Model
- The controller is created and gets references to the model and the view; it observes the model
- The application starts event processing
In case of a user event, the following steps happen:
- The controller accepts the user input, handles them, and triggers the model
- The model changes its internal data
- The model notifies all views and controllers about the change of the internal data
- The views and controller update themself
- If the PIN is entered incorrectly in an ATM for the third time, this can mean: The display shows that your account is locked. The ATM confiscates your debit card
- The controller continues to process events
The following program
mvc.cpp applies the MVC.
The controller gets its model and view in its constructor (line 1) and displays its defect list
defects_ (line 5) in three ways (lines 2 – 4). The controller triggers each call in the function and uses its view to display the data given by the model (lines 6 – 8).
The following screenshot shows the output of the program:
Presentation-Abstraction-Control is an additional pattern from “Pattern-Oriented Software Architecture, Volume 1“, similar to the MVC. It used a hierarchical structure of agents, each agent consisting of the presentation, abstraction(model), and data control. The agents communicate with each other through the control.
Pros and Cons
- Separation of concern: The Model is strictly separated from the Controller and the Views. Therefore, many views or controllers can be supported concurrently and changed at run time.
- The views as synchronized because they are updated at the same time.
- The MVC may be too complex and overkill for a small human-machine interface.
- A modification in the model may trigger a cascade of operations on its dependent views and controllers.
- The view and the controller are strongly coupled. A modification of one of them may break the other.
Event-driven applications, such as GUIs or servers, often apply the architecture pattern Reactor. A Reactor can accept multiple requests simultaneously and distribute them to different handlers.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,