Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't Cocoa MVC really MVP?

Once again, an MVC-related question. A few days ago I started reading the Cocoa Fundamentals Guide from Apple in which Apple explains their implementation of MVC.

In the chapter MVC as a Compound Design Pattern (link), they compare two MVC-versions:

  • The old / traditional SmallTalk version: enter image description here

  • The current Apple-defined version: enter image description here

They describe this current model as follows:

The controller object in this compound design pattern incorporates the Mediator pattern as well as the Strategy pattern; it mediates the flow of data between model and view objects in both directions. Changes in model state are communicated to view objects through the controller objects of an application.

The traditional pattern looks like MVC, nothing wrong. But the name of their current pattern confuses me. In my knowledge this could be seen as plain MVP because the Controller always seems to mediate between View and Model.

Am I completely wrong, do I misunderstand MVC or MVP? Or did Apple just use the wrong name for this pattern? And more importantly, WHY is this current pattern called MVC?

like image 203
Thomas Smith Avatar asked Feb 18 '13 19:02

Thomas Smith


2 Answers

You're not wrong, but nor is the author of the Apple documentation.

The history of MVC is now long and complex -- not least because many systems advocated a tripartite separation which actually muddling the controller into the model or mixing the controller into the view. From the very early Smalltalk implementations, it became clear that keeping model information out of the view was a very good thing, and that this was fairly easy to do.

Cleanly separating the responsibilities of the controller and the view, on the other hand, is much less straightforward. Lots of views want to be reused, like buttons or textfields. The reusable part of their controllers wants to be reused too. But you don't PUSH a textfield or BOLD a button; lots of the button behavior is tied pretty closely to the view. At the same time, it can be hard to be sure when a business rule belongs in the model and when it belongs in the controller.

In addition, this (very good) Apple document is trying to capture a philosophical design idea, not describing The One True Way. Lots of Cocoa controllers subsystems look a lot like traditional MVC. Traditional cocoa deemphasized controllers, so this document is, in essence, arguing to give them a place as mediators between (reusable) views and (potentially reusable) models.

Lots of Cocoa implementors prefer thin controllers, essentially working as façades to decouple view and model.

like image 76
Mark Bernstein Avatar answered Oct 15 '22 04:10

Mark Bernstein


Since MVP is a subset of MVC it's not surprising to find it in MVC systems. Yes, that second diagram illustrates the MVP pattern.

Apple calls it a mediating controller which – I synthesize – is just another name for MVP.

Frankly I'm not sure the term MVP ought to catch on. It implies it's a totally different pattern, and presenter seems focused on the UI, when sometimes it's just a relationship between model and controller. Mediating controller describes the distinction quite simply.

I had to look up MVP even to know what the heck you were asking about. The term was used in a paper in 1996. When OS X was released it would still have been new.

like image 20
paulmelnikow Avatar answered Oct 15 '22 04:10

paulmelnikow