Java Omnibus
OOAD & Architecture / J2EE / Jakarta EE Patterns / Composite View Pattern
J2EE Presentation Java Order Management
Presentation

Composite View Pattern

Composite View assembles a page from reusable presentation fragments so headers, sidebars, order summaries, alerts, and dashboards remain consistent across screens.

Why This Topic Matters

Why it matters in enterprise Java

Commerce systems often repeat account widgets, cart summaries, nav sections, and status panels. Composite View avoids hand-copying those pieces into every page.

Pattern Essentials

Intent, problem, and solution

Intent

What the pattern is trying to achieve

Compose a full page from reusable subviews while keeping the overall layout consistent.

Problem

What goes wrong without it

Large pages repeat the same fragments and layout rules, causing duplication and inconsistent updates.

Solution

How the pattern answers the problem

Break the page into reusable fragments that a composing view assembles into one response.

Modern Relevance

Why it still matters now

The pattern remains visible in Thymeleaf fragment layouts, reusable JSP includes, server-side UI components, and shared web design systems.

Structure

How the collaboration works

  • A controller prepares one page-level model.
  • The rendering layer includes reusable subviews or fragments.
  • Fragments focus on one display concern such as summary cards or alert banners.
  • The composed page remains consistent while fragments stay reusable.
Java Example

Java example in the Order Management domain

Java example: page model prepared for reusable page fragments
package org.javaomnibus.ecommerce.view;

import java.util.List;

public record AccountDashboardView(
    CustomerSummary customer,
    OrderSummary recentOrder,
    List<AlertMessage> alerts
) {}

// A template can render header, summary-card, alert-list, and order-panel
// using this page model and shared reusable fragments.
Code Walkthrough

Step by step

  1. The page-level model collects the data the full screen needs.
  2. Fragments render reusable subsections such as summary cards or alerts.
  3. The controller does not need to handcraft every repeated layout concern.
  4. Fragment reuse keeps the interface more consistent across related pages.
Real-World Usage

Where this pattern shows up

  • Dashboards, account pages, and admin consoles
  • Shared headers, side panels, and order widgets
  • Component-based server-rendered UI systems
When Not To Use

Cases where another shape is better

  • Tiny pages with no meaningful shared composition
  • Cases where composition is so dynamic that a richer component system is needed
Trade-Offs

Trade-offs and design pressure

  • Too much fragmentation can make view flow harder to follow.
  • Reusable fragments need clear ownership to avoid inconsistent variants.
Comparison Note

How this differs from nearby patterns

Composite View focuses on assembling reusable view fragments. View Helper focuses on formatting. Service to Worker focuses on request-to-service-to-view flow.

Common Misuse

Mistakes to avoid

  • Mixing business logic into fragments
  • Over-fragmenting simple pages into dozens of tiny templates
  • Skipping a cohesive page model and making fragments fetch data on their own
Related Pages

Related concepts


What To Read Next