BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Design Patterns - Model View Controller (MVC) Pattern

Patterns.png




Bookmark and Share





bogotobogo.com site search:

Model View Controller (MVC) Pattern

  1. Separation of Model from View components makes it possible to implement several user interfaces that reuse the common core business logic.
  2. Duplication of low-level Model code is eliminated across multiple UI implementations.
  3. Decoupling of Model and View code results in an improved ability to write unit tests for the core business logic code.
  4. Modularity of components allows core logic developers and UI developers to work simultaneously without affecting the other.
Basically, it says that there are three distinct responsibilities for our application.
mvc_diagram

  1. Model
    1. Manages the app data and state
    2. Not concerned with UI or presentation
    3. Often persists somewhere
    4. Same model should be reusable, unchanged in different interfaces

  2. View
    1. Present the Model to the user in an appropriate interface
    2. Allows user to manipulate data
    3. Does not store any data except to cache state
    4. Easily reusable & configurable to display different data

  3. Controller
    1. Intermediary between Model & View
    2. Updates the view when the model changes
    3. Updates the model when the user manipulates the view
    4. Typically where the app logic lives

This pattern decouples changes to how data are manipulated from how they are displayed or stored, while unifying the code in each component. In other words, it's a way of developing apps keeping the data (model) used in the program, and the visual (view) component of the program separate from one another, each interacting only with a controller containing the logic of our program. The view and the model interact only with the controller NEVER with each other.

A triad of three modules linked by the Observer Pattern. The View drives a presentation and elements within the View observe the Model. Elements within the Controller observe the View and Model, and elements within the Model observe the Controller.

In a simple application, the Controller can affect changes to the Model based on user input and also communicate those changes to the View so that the UI can be updated. In real-world applications, however, the View will normally also need to update to reflect additional changes to the underlying Model. This is necessary because changing one aspect of the Model may cause it to update other dependent Model state. This requires Model code to inform the View layer when state changes happen. The Model code, however, cannot statically bind and call the View code. This is where observers come in.

The Observer Pattern is a specific instance of the publish/subscribe paradigm. These techniques define a one-to-many dependency between objects such that a publisher object can notify all subscribed objects of any state changes without depending on them directly.

MVC is often seen in web applications where the view is the HTML or XHTML generated by the app. The controller receives GET or POST input and decides what to do with it, handing over to domain objects (i.e. the model) which contain the business rules and know how to carry out specific tasks such as processing a new subscription, and which hand control to (X)HTML-generating components such as templating engines, XML pipelines, AJAX callbacks, etc.

The MVC model can be found in UI toolkits such as Nokia's Qt, Apple's Cocoa, Java Swing, and MFC library.



Model View Controller (MVC) Pattern in iPhone SDK

When we write an iPhone app, we have to deal with MVC all the time. iPhone SDK Tutorials


mvc1

The Model talks to the controller and the controller can manipulate the model. The View can manipulate the Controller and the Controller can set the value of the View state. Note that we do not have any arrow between the Model and the View.



mvc2

We have a Controller in the middle with Model in the left and the view in the right side. The controller has outlets so that it can talk to the View and Model. The View may have buttons or sliders that can send the actions. Essentially, actions go from the window to the Controller so that the Controller can do whatever it needs.





Model View Controller (MVC) Pattern in Android architecture

Models: Content Providers.
Data Managers that are the recommended form of inter-application data sharing.

Views: Activities.
This is the application's primary user interface component. Every individual screen of an Android application is derived from the Activity Java class (android.app.Activity).
They are containers for Views (android.view.View).

Controllers: Services.
These are background components that behave like UNIX daemons and Windows services. They run invisibly and perform ongoing unattended processing.




MVC in Django

Python's web service framework also based on MVC.


Django_MVC.png

For more info, visit MVC - Hello World Django.




Useful References
  1. Martin Fowler's GUI Architectures
  2. Qt 5 - Model/View Programming.
    Qt's MVC is not exactly follow conventional MVC (if there is any clear cut), but it works wonderfully within Qt's framework. Their Controller is merged into View.
    If the view and the controller objects are combined, the result is the model/view architecture. This still separates the way that data is stored from the way that it is presented to the user, but provides a simpler framework based on the same principles. This separation makes it possible to display the same data in several different views, and to implement new types of views, without changing the underlying data structures. To allow flexible handling of user input, we introduce the concept of the delegate. The advantage of having a delegate in this framework is that it allows the way items of data are rendered and edited to be customized.
    However, it's worth investigating their approach of using "delegate" with their Model/View pattern.
  3. Qt 5 MVC - ModelView with QListView and QStringListModel
  4. Qt 5 MVC - ModelView with QTreeView and QDirModel
  5. Qt 5 MVC - ModelView with QTreeView and QFileSystemModel
  6. Qt 5 MVC - ModelView with QTableView and QItemDelegate

  7. There are MVC patterns that can be found in other programming languages. The followings are my tutorials for Web Services Framework:
  8. Django - RESTful Web Framework (Python)
  9. Laravel - RESTful Web Framework (PHP)
  10. Ruby on Rails - RESTful Web Framework (Ruby)







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong





List of Design Patterns



Introduction

Abstract Factory Pattern

Adapter Pattern

Bridge Pattern

Chain of Responsibility

Command Pattern

Composite Pattern

Decorator Pattern

Delegation

Dependency Injection(DI) and Inversion of Control(IoC)

Façade Pattern

Factory Method

Model View Controller (MVC) Pattern

Observer Pattern

Prototype Pattern

Proxy Pattern

Singleton Pattern

Strategy Pattern

Template Method Pattern

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







C++ Tutorials

C++ Home

Algorithms & Data Structures in C++ ...

Application (UI) - using Windows Forms (Visual Studio 2013/2012)

auto_ptr

Binary Tree Example Code

Blackjack with Qt

Boost - shared_ptr, weak_ptr, mpl, lambda, etc.

Boost.Asio (Socket Programming - Asynchronous TCP/IP)...

Classes and Structs

Constructor

C++11(C++0x): rvalue references, move constructor, and lambda, etc.

C++ API Testing

C++ Keywords - const, volatile, etc.

Debugging Crash & Memory Leak

Design Patterns in C++ ...

Dynamic Cast Operator

Eclipse CDT / JNI (Java Native Interface) / MinGW

Embedded Systems Programming I - Introduction

Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu and Fedora

Embedded Systems Programming III - Eclipse CDT Plugin for gcc ARM Toolchain

Exceptions

Friend Functions and Friend Classes

fstream: input & output

Function Overloading

Functors (Function Objects) I - Introduction

Functors (Function Objects) II - Converting function to functor

Functors (Function Objects) - General



Git and GitHub Express...

GTest (Google Unit Test) with Visual Studio 2012

Inheritance & Virtual Inheritance (multiple inheritance)

Libraries - Static, Shared (Dynamic)

Linked List Basics

Linked List Examples

make & CMake

make (gnu)

Memory Allocation

Multi-Threaded Programming - Terminology - Semaphore, Mutex, Priority Inversion etc.

Multi-Threaded Programming II - Native Thread for Win32 (A)

Multi-Threaded Programming II - Native Thread for Win32 (B)

Multi-Threaded Programming II - Native Thread for Win32 (C)

Multi-Threaded Programming II - C++ Thread for Win32

Multi-Threaded Programming III - C/C++ Class Thread for Pthreads

MultiThreading/Parallel Programming - IPC

Multi-Threaded Programming with C++11 Part A (start, join(), detach(), and ownership)

Multi-Threaded Programming with C++11 Part B (Sharing Data - mutex, and race conditions, and deadlock)

Multithread Debugging

Object Returning

Object Slicing and Virtual Table

OpenCV with C++

Operator Overloading I

Operator Overloading II - self assignment

Pass by Value vs. Pass by Reference

Pointers

Pointers II - void pointers & arrays

Pointers III - pointer to function & multi-dimensional arrays

Preprocessor - Macro

Private Inheritance

Python & C++ with SIP

(Pseudo)-random numbers in C++

References for Built-in Types

Socket - Server & Client

Socket - Server & Client 2

Socket - Server & Client 3

Socket - Server & Client with Qt (Asynchronous / Multithreading / ThreadPool etc.)

Stack Unwinding

Standard Template Library (STL) I - Vector & List

Standard Template Library (STL) II - Maps

Standard Template Library (STL) II - unordered_map

Standard Template Library (STL) II - Sets

Standard Template Library (STL) III - Iterators

Standard Template Library (STL) IV - Algorithms

Standard Template Library (STL) V - Function Objects

Static Variables and Static Class Members

String

String II - sstream etc.

Taste of Assembly

Templates

Template Specialization

Template Specialization - Traits

Template Implementation & Compiler (.h or .cpp?)

The this Pointer

Type Cast Operators

Upcasting and Downcasting

Virtual Destructor & boost::shared_ptr

Virtual Functions



Programming Questions and Solutions ↓

Strings and Arrays

Linked List

Recursion

Bit Manipulation

Small Programs (string, memory functions etc.)

Math & Probability

Multithreading

140 Questions by Google



Qt 5 EXPRESS...

Win32 DLL ...

Articles On C++

What's new in C++11...

C++11 Threads EXPRESS...

Go Tutorial

OpenCV...








Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master