12

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?

1
  • It kinda depends. If your "views" are just dumb templates and model is just a collection of active record instances, then what you have is not even MVP. It is just a clone of Rails.
    – tereško
    Feb 18, 2013 at 19:53

2 Answers 2

11

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.

2
  • Good answer. Even good parts of Cocoa cross these lines. NSImage for performance reasons encapsulates controller behaviors like faulting and caching, and an NSImageView might ask an image for a resized representation without consulting a mediating controller. Also it's perfectly reasonable to have NSImageView which is a view built for a specific model class. Also, that Apple doc mentions Cocoa Bindings but suggests they're unmediated connections between view and model. I usually just think of the bindings themselves as mediating controllers, especially considering the message flow. Feb 18, 2013 at 20:03
  • Nice answer. I see it is more about showing what is good practice in general then about defining an exact and always right way to design things. MVC is not stuck in one definition, instead it demonstrates a flexible/abstract idea of how applications can be developed. Thanks for your time. Feb 18, 2013 at 21:20
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.