MVC pattern

MVC is architectural pattern that separates an application into three main logical components Model, View, and Controller.

Model is responsible to manage data, that independent from the user interface.

View is responsible for rendering UI elements, accept user input and pass it to the controller. View may observe the model data directly to update itself.

Controller is responsible to accept events from View and Model then converts them to commands for the Model or View. Controller may be responsible to update View. Controller chooses which View is shown to the user.

MVC model was first introduced in 1987 in the Smalltalk programming language. At 1988 MVC was first time accepted as a general concept.

advantages

1. Development of the various components can be performed parallelly.

2. Model component can be tested separately from the user.

3. Code maintenance easy to extend and grow.

drawback

1. Controller may have lots of codes.

2. Increases the event-driven nature of the user-interface code, which can become more difficult to debug.

3. From previous, if the model undergoes frequent changes, the views could be flooded with update requests. Or views may take some time to render when the model data loading takes long time.

example

Many frameworks related with UI follow this pattern. For example, Sping MVC, android/ios GUI, desktops GUI like JavaFx.

// Controller in Spring Boot 
@Controller
@RequestMapping("/articles" )
class ContentController {

 @Autowired
 lateinit var srv: ContentsService
     
  @GetMapping("/showLst/{idcateg}")
  fun showLst(@PathVariable idCateg: Long, @RequestParam mode: Int): ModelAndView =
        ModelAndView("/articlelist").apply {
            addObject( "some_model_name", srv.loadData(idCateg, mode))
   }
}

What is happen here? When user makes the /showLst/{idcateg} request Controller create View with articlelist name. Then Conroller collect the necessary model data and pass them to the View. There are may be more methods for other requests with other views. Controller may pass direct references to the model objects.

Similarly, in Android, the Activity class acts as a controller. It chooses which View is shown to the user. View passes user input to the Activity. View may observe the model data directly to update itself.