||This article needs attention from an expert in Computer science. (March 2012)|
Hierarchical model–view–controller (HMVC) is a software architectural pattern, a variation of Model–view–controller (MVC) similar to Presentation-abstraction-control (PAC), that was published in 2000 in an article in JavaWorld Magazine, the authors apparently unaware of PAC which was published 13 years earlier.
The controller has some oversight. The controller selects the model and then selects the view, so there is an approval mechanism by the controller. The model prevents the view from accessing the data source directly.
The largest practical benefit of using an HMVC architecture is the "widgetization" of content structures. An example might be comments, ratings, Twitter or blog RSS feed displays, or the display of shopping cart contents for an e-commerce website. It is essentially a piece of content that needs to be displayed across multiple pages, and possibly even in different places, depending on the context of the main HTTP request.
Traditional MVC frameworks generally don't provide a direct answer for these types of content structures, so people generally end up duplicating and switching layouts, using custom helpers, creating their own widget structures or library files, or pulling in unrelated data from the main requested Controller to push through to the View and render in a partial. None of these are particularly good options, because the responsibility of rendering a particular piece of content or loading required data ends up leaking into multiple areas and getting duplicated in the places it is used.
HMVC, or specifically the ability to dispatch sub-requests to a Controller to handle these responsibilities is the obvious solution. If you think about what you're doing, it fits the Controller structure exactly. You need to load some data about comments, and display them in HTML format. So you send a request to the comments Controller with some params, it interacts with the Model, picks a View, and the View displays the content. The only difference is you want the comments displayed inline, below the blog article the user is viewing instead of a completely separate full comments page (though with an HMVC approach, you can actually serve both internal and external requests with the same controller and "kill two birds with one stone", as the saying goes). In this regard, HMVC is really just a natural byproduct of striving for increased code modularity, re-usability, and maintaining a better separation of concerns.
- Jason Cai, Ranjit Kapila, and Gaurav Pal (July 2000). "HMVC: The layered pattern for developing strong client tiers". JavaWorld. Retrieved 2006-05-25.
- "TP" (2000). "Is HMVC PAC? (letter to the editor)". JavaWorld. Archived from the original on 2005-02-05. Retrieved 2006-05-25.
- Vance Lucas (April 2011). "What is the HMVC pattern?". StackOverflow. Retrieved 2013-10-15.