The MVC Design Pattern
Designing a MVC Calculator
Learning outcomes
- We will learn about applying the Model View Controller (MVC) design pattern:
- You will be able to identify objects belonging to three distinct types of classes
- You will be able to understand the rules for object interaction
- You will be able to apply this pattern to achieve high cohesion and low coupling
Consider the Calculator user story again:
The user needs a calculator with simple integer arithmetic operations. He wishes to input the numbers and the desired operation into the program, and expects a correct answer for this calculation. For quality reasons, the input and output should be easy to read out. S/he is not concerned with large numbers, and expects to use it for both positive and negative integers
We will now apply the MVC pattern to this example
- Layered approach:
- Model layer contains all classes that are used for storing and manipulating the persistent data Entity Classes
- View layer contains all the classes for user interaction including HMI, GUI and text and any other types. View Classes
- Controller layer contants a set of classes, which orchestrate the other classes in a desired sequence. Controller Classes
MVC pattern rules
- Actors can interact with boundary classes
- When actors are humans boundary classes can be CLI, GUI or HMI.
- When actors are other machines, actors can be pipes or sockets
- Controllers can interact with any classes
- Entities can interact with controllers or other entities
- Boundary classes can interact with controllers
Calculator MVC objects and their interaction
Run-time interaction using sequence diagram
Next we will do an ACP Exercise
- Given the pictorial design using MVC
- write Java code for the classes
- implement the controller classes for ensuring the correct object interaction as per the sequence diagram