MVC, MVP, MVVM

Three patterns that separate the interface from the business logic: the role of the model, the view, the controller, the presenter and the view-model, the direction of dependencies and what sets the three apart.

  • 28 cards
  • 4 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. On the diagram

A few cards from the deck

  • Separation of concerns: what is it?

    Show the answer

    The principle of splitting a program into parts that each deal with a single subject: data and rules, display, reaction to gestures.

    When the look of a screen changes, neither the calculation rules nor the data access should need touching: MVC, MVP and MVVM are three ways of applying this principle to an interface.

  • What does this MVC-structured program print?

    ```python
    class Panier:
    def __init__(self):
    self.articles = []
    def ajouter(self, prix):
    self.articles.append(prix)
    def total(self):
    return sum(self.articles)

    class Vue:
    def afficher(self, total):
    print("Total :", total)

    class Controleur:
    def __init__(self, panier, vue):
    self.panier = panier
    self.vue = vue
    def clic_ajouter(self, prix):
    self.panier.ajouter(prix)
    self.vue.afficher(self.panier.total())

    c = Controleur(Panier(), Vue())
    c.clic_ajouter(10)
    c.clic_ajouter(5)
    `

    • Total : 10 then Total : 15
    • Total : 10 then Total : 5
    • Total : 15 twice in a row
    • Total : 10 then Total : 10
    Show the answer

    Total : 10 then Total : 15

    The basket keeps its state from one click to the next: the second call adds 5 to the 10 already there. The view only displays the total the model computes.

  • In MVC, the model knows neither the view nor the controller: the … observes the model, and the … passes on the user's actions.

    Show the answer

    In MVC, the model knows neither the view nor the controller: the view observes the model, and the controller passes on the user's actions.

    The direction of dependencies is what matters: the model is at the centre and needs nobody, the other parts depend on it.

  • Model: what is it?

    Show the answer

    The part that holds the application's data and its business rules, knowing nothing about the screen.

    Since it ignores the display, the same object can serve a web page, a mobile app or an automated test without any change.