Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Web application components made easy with Composite View

Implement Web applications featuring pluggable components with the Composite View design pattern

  • Print
  • Feedback
In Core J2EE Patterns: Best Practices and Design Strategies, Deepak Alur, John Crupi, and Dan Malks identify 15 J2EE (Java 2 Platform, Enterprise Edition) design patterns, of which the Composite View pattern proves to be one of the most compelling. The Composite View pattern builds composite views from multiple subviews, with the potential to greatly simplify application development by allowing content and layout to be plugged into a framework.

Before the Composite View pattern was widely known, developers knew it as JSP (Java Server Pages) templates; a term that originated in Sun's J2EE Blueprints. Templates combine the Composite and Strategy patterns (both espoused by the Gang of Four (GOF) in Design Patterns) to separate both content and layout from JSPs. Such separation defines a JSP programming style that lets page authors and software developers work, for the most part, independently.

The templates description in J2EE Blueprints motivated me to write a templates custom tag library, which I contributed to the Apache Struts project in fall 2000. Not long after that, I wrote about that tag library for JavaWorld in "JSP Templates" (September 2000). Consequently, feedback from both JavaWorld readers and Struts users prompted me to rewrite the templates tag library from scratch for my book Advanced JavaServer Pages.

In spring 2001, I contributed some code from the rewritten templates tag library -- now known as the regions tag library -- to illustrate the Composite View J2EE design pattern in the aforementioned Core J2EE Patterns: Best Practices and Design Strategies.

This article discusses the Composite View pattern by exploring the regions tag library in more detail. Also, because the tag library examined in this article is a second revision of the library discussed in JavaWorld's "JSP Templates," you can view this article as somewhat of a sequel.

The Composite View pattern

Nearly all modern object-oriented development environments support three key objects for flexible and extensible graphical user interface (GUI) development: components, containers, and layout managers. Components represent graphic objects such as text, buttons, or lists. Containers maintain component lists, while layout managers position and size (or lay out) a container's components.

Developers typically implement components and containers with the GOF Composite design pattern, which lets you compose graphical objects into tree hierarchies. Meanwhile, layout managers are typically implemented with the GOF Strategy design pattern, so that you can change a container's layout strategy at runtime without changing the container itself. For its part, the J2EE Composite View design pattern acts as a recipe for components, containers, and layout managers for JSP-based Web applications.

At the low end, the Composite View pattern resembles the GOF Composite pattern. In describing the Composite View pattern in Core J2EE Design Patterns, Alur, Crupi, and Malks write, "Use composite views that are composed of multiple atomic subviews...," which means your views should be composites that can contain other views -- an endorsement of the GOF Composite pattern. Those views can be composites also, so you can arrange views in a tree hierarchy.

  • Print
  • Feedback

Resources