|
|
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
Page 2 of 5
So which Composite pattern implementation—the one in Figure 1 or the one in Figure 2—works best? That's always a topic of great debate among Composite pattern implementers; Design Patterns prefers the Figure 2 implementation because you never need to distinguish between components and containers, and you never need to perform a cast. Personally, I prefer Figure 1's implementation, because I have a strong aversion to implementing methods in a class that don't make sense for that object type.
Now that you understand the Composite pattern and how you can implement it, let's examine a Composite pattern example with the Apache Struts JavaServer Pages (JSP) framework.
The Apache Struts framework includes a JSP tag library, known as Tiles, that lets you compose a Webpage from multiple JSPs. Tiles is actually an implementation of the J2EE (Java 2 Platform, Enterprise Edition) CompositeView pattern, itself based on the Design Patterns Composite pattern. Before we discuss the Composite pattern's relevance to the Tiles tag library, let's first review the rationale for Tiles, and how you use it. If you're already familiar with Struts Tiles, you can skim the following sections and commence reading at "Use the Composite Pattern with Struts Tiles."
Note: You can read more about the J2EE CompositeView pattern in my "Web Application Components Made Easy with Composite View" (JavaWorld, December 2001) article.
Designers often construct Webpages with a set of discrete regions; for example, Figure 3's Webpage comprises a sidebar, header, content region, and footer.
Figure 3. The Composite pattern and Struts Tiles. Click on thumbnail to view full-size image.
Websites often include multiple Webpages with identical layouts, such as Figure 3's sidebar/header/content/footer layout. Struts Tiles lets you reuse both content and layout among multiple Webpages. Before we discuss that reuse, let's see how Figure 3's layout is traditionally implemented with HTML alone.
Example 1 shows how you can implement Figure 3's Webpage with HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page contentType='text/html; charset=UTF-8' %>
<html>
<head>
<title>Implementing Complex Layouts by Hand</title>
</head>
<body background='graphics/blueAndWhiteBackground.gif'>
<%-- One table lays out all of the content for this page --%>
<table width='100%' height='100%'>
<tr>
<%-- Sidebar section --%>
<td width='150' valign='top' align='left'>
<table>
<tr>
<%-- Sidebar top --%>
<td width='150' height='65' valign='top' align='left'>
<a href=''>
<img src='graphics/flags/britain_flag.gif'/></a>
<a href=''>
<img src='graphics/flags/german_flag.gif'/></a>
<a href=''>
<img src='graphics/flags/chinese_flag.gif'/></a>
</td>
</tr>
<tr>
<%-- Sidebar bottom --%>
<td>
<font size='5'>Links</font><p>
<a href=''>Home</a><br>
<a href=''>Products</a><br>
<a href=''>Downloads</a><br>
<a href=''>White papers</a><br>
<a href=''>Contact us</a><br>
</td>
</tr>
</table>
</td>
<%-- Main content section --%>
<td valign='top' height='100%' width='*'>
<table width='100%' height='100%'>
<tr>
<%-- Header section --%>
<td valign='top' height='15%'>
<font size='6'>Welcome to Sabreware, Inc.</font>
<hr>
</td>
<tr>
<tr>
<%-- Content section --%>
<td valign='top' height='*'>
<font size='4'>Page-specific content goes here</font>
</td>
</tr>
<tr>
<%-- Footer section --%>
<td valign='bottom' height='15%'>
<hr>
Thanks for stopping by!
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
The preceding JSP has two major drawbacks: First, the page's content is embedded in the JSP, so you cannot reuse any of it,
even though the sidebar, header, and footer are likely to be the same across many Webpages. Second, the page's layout is also
embedded in that JSP, so you likewise cannot reuse it even though many other Webpages in the same Website use the same layout.
We can use the <jsp:include> action to remedy the first drawback, as I discuss next.