Although the majority of modern-day JavaScript frameworks attempt to evolve the MVC paradigm to better fit the differing needs of web application development, there is one framework that attempts to adhere to the pure form of the pattern found in Smalltalk-80. Maria.js (https://github.com/petermichaux/maria) by Peter Michaux offers an implementation that is faithful to MVCâs origins: Models are models, Views are views and Controllers are nothing but controllers. While some developers might feel an MV* framework should address more concerns, this is a useful reference to be aware of in case you would like a JavaScript implementation of the original MVC.
At this point in the book, we should have a basic understanding of what the MVC pattern provides, but thereâs still some fascinating information about it worth noting.
The GoF (Gang of Four) The GoF do not refer to MVC as a design pattern, but rather consider it a set of classes to build a user interface. In their view, itâs actually a variation of three classical design patterns: the Observer, Strategy, and Composite patterns. Depending on how MVC has been implemented in a framework, it may also use the Factory and Template patterns. The GoF book mentions these patterns as useful extras when working with MVC.
As we have discussed, models represent application data, while views represent what the user is presented with on screen. As such, MVC relies on the Observer pattern for some of its core communication (something that, surprisingly, isnât covered in many articles about the MVC pattern). When a model is changed, it notifies its observers (Views) that something has been updatedâthis is perhaps the most important relationship in MVC. The observer nature of this relationship is also what facilitates multiple views being attached to the same model.
For developers interested in knowing more about the decoupled nature of MVC (once again, depending on the implementation), one of the goals of the pattern is to help define one-to-many relationships between a topic and its observers. When a topic changes, its observers are updated. Views and controllers have a slightly different relationship. Controllers facilitate views to respond to different user input and are an example of the Strategy pattern.
Having reviewed the classical MVC pattern, we should now understand how it allows us to cleanly separate concerns in an application. We should also now appreciate how JavaScript MVC frameworks may differ in their interpretation of the MVC pattern, which although quite open to variation, still share some of the fundamental concepts the original pattern has to offer.
When reviewing a new JavaScript MVC/MV* framework, remember: it can be useful to step back and review how itâs opted to approach architecture (specifically, how it supports implementing models, views, controllers or other alternatives), as this can better help us grok how the framework expects to be used.
Get Learning JavaScript Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.