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.
- Concepts
- Questions
- Sort and link
- 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 : 10thenTotal : 15Total : 10thenTotal : 5Total : 15twice in a rowTotal : 10thenTotal : 10
Show the answer
Total : 10thenTotal : 15The 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.