📑 Table of Contents
The structure of an application with PAC.

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[1] in JavaWorld Magazine. The authors were apparently unaware of PAC, which was published 13 years earlier.[2]

The controller has some oversight in that it selects first the model and then the view, realizing an approval mechanism by the controller. The model prevents the view from accessing the data source directly.

Example

edit

The largest practical benefit of using an HMVC architecture is the "widgetization" of content structures.[3] 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 do not provide a direct answer for these types of content structures, so programmers often 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. The downside is that the responsibility of rendering a particular piece of content or loading required data leaks into multiple areas and gets duplicated in the respective places.

HMVC, or specifically the ability to dispatch sub-requests to a Controller to handle these responsibilities aims to solve this problem. The structure mimics that of traditional MVC. For example, if one needs to load some data about comments, and display them in HTML format, one would send a request to the comments Controller with some parameters. The request then interacts with the Model, picks a View, which displays the content. The difference from a traditional MVC is that instead of displaying the comments in a fully separate page, they are displayed inline below the article the user is viewing. In this regard, HMVC strives to increase code modularity, aid reusability, and maintain a better separation of concerns.

The following is a code example demonstrating sub-requests to child controllers (widgets).

package org.wikipedia.examples;

import java.util.Arrays;
import java.util.List;

record Comment(String author, String text) {}

class CommentRepository {
    public List<Comment> findByArticleId(int articleId) {
        // Return some commments found in the article (example)
        return Arrays.asList(
            new Comment("Alice", "Great article!"),
            new Comment("Bob", "I learned a lot.")
        );
    }
}

class CommentsController {
    private final CommentRepository repository = new CommentRepository();

    public String handle(int articleId) {
        List<Comment> comments = repository.findByArticleId(articleId);
        return renderCommentsHtml(comments);
    }

    private String renderCommentsHtml(List<Comment> comments) {
        StringBuilder sb = new StringBuilder();
        sb.append("<div class='comments'>");
        for (Comment c : comments) {
            sb.append("<p><strong>")
              .append(c.getAuthor())
              .append(":</strong> ")
              .append(c.getText())
              .append("</p>");
        }
        sb.append("</div>");
        return sb.toString();
    }
}

public class ArticleController {
    private final CommentsController commentsController = new CommentsController();

    public String showArticlePage(int articleId) {
        // Main article content
        String articleHtml =
                "<h1>HMVC Demo Article</h1>" +
                "<p>This is the main article.</p>";

        String commentsWidget = commentsController.handle(articleId);
        return articleHtml + commentsWidget;
    }

    public static void main(String[] args) {
        ArticleController controller = new ArticleController();
        String html = controller.showArticlePage(42);
        System.out.println(html);
    }
}

See also

edit

References

edit
  1. ^ Cai, Jason; Kapila, Ranjit; Pal, Gaurav (July 21, 2000). "HMVC: The layered pattern for developing strong client tiers". JavaWorld. Retrieved 2020-07-18.
  2. ^ "TP" (September 8, 2000). "Is HMVC PAC?". Letters to the editor. JavaWorld. Archived from the original on 2006-03-19.
  3. ^ Vance Lucas (April 2011). "why HMVC pattern?". StackOverflow. Retrieved 2013-10-15.

📚 Artikel Terkait di Wikipedia

Model–view–controller

evolved, giving rise to variants such as hierarchical model–view–controller (HMVC), model–view–adapter (MVA), model–view–presenter (MVP), model–view–viewmodel

CodeIgniter

CodeIgniter can be also modified to use Hierarchical Model View Controller (HMVC) which allows the developers to maintain modular grouping of Controller,

FuelPHP

open-source web application framework written in PHP which implements the HMVC pattern. The FuelPHP project commenced in October 2010, with its major contributors

Hasna Sal

Retrieved 20 December 2022. Gallery, HMVC. "HMVC Stage Room". HMVC Gallery. Gallery, HMVC. "HMVC Stage Room". HMVC Gallery. Sykes, Krista. "Summer Reading

Extended Position Description

full move number which are not mandatory, but implemented as operations hmvc and fmvn. It is intended for data and command interchange among chessplaying

ContentBox Modular CMS

created by Ortus Solutions, Corp. ContentBox has been designed as a modular HMVC software based on Hibernate ORM and the ColdBox Platform. ContentBox Modular

Comparison of server-side web frameworks

Memcache, XCache, WinCache, and Filesystem Yes No ? ? FuelPHP ≥ 5.3.3 Yes MVC, HMVC Push Yes Yes PHPUnit Yes Yes, Plugins available Yes, Plugins available File

Presentation–abstraction–control

OpenWengo community. Cai, Jason; Kapila, Ranjit; Pal, Gaurav (July 21, 2000). "HMVC: The layered pattern for developing strong client tiers". JavaWorld. Retrieved