Name

Composite View

Goal

Building a view from multiple reusable subviews (Figure A-7).

Classes in the Composite View pattern
Figure A-7. Classes in the Composite View pattern

Participants

View

A common interface implemented by both composite and leaf views.

Leaf view

A view responsible for data that is actually displayed.

Composite view

A view responsible for managing other views, including both leaves and other composites (Figure A-8).

Interactions in the Composite View pattern
Figure A-8. Interactions in the Composite View pattern

Interactions

The dispatcher initializes a set of leaf views based on request or navigation data. The dispatcher then forwards control to a composite view, which includes multiple elements with generic names, such as leaf1. The correct leaf view, for example view1.html, is substituted for each generic name. The composite passes control to each leaf view in turn, each of which generates a portion of the final view.

Notes

Simple composite views can be implemented in a number of ways, including JSP include directives. A more flexible approach is to use templates that refer to leaves by generic names, allowing reuse of the template with different sets of leaves. JSP custom tags are usually the best way to implement templates. Templates are also included in the the Apache Struts “tile” mechanism.

Get J2EE 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.