|
|
YOUR FEEDBACK
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today! Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Feature
J2EE Design Patterns
By: Alan Baumgarten
Digg This!
Not surprisingly, the concept of design patterns originated in the world of architecture. A design pattern is a proven, reusable solution to a recurring problem. I've always enjoyed building things, both as a software engineer and as a homeowner. Whether the raw materials are Java classes or plywood and nails, I get the best finished results when I start with a good design and follow standard procedures that reflect the experience and knowledge of seasoned professionals. When I wear my tool belt, I try to follow these "tricks of the trade." As a software developer, I use design patterns. Software design patterns were initially popularized by the book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. The four authors are often referred to as the "Gang of Four," or simply GOF. Sun's Java Center has cataloged a number of design patterns specifically for J2EE applications (see http://java.sun.com/blueprints/corej2eepatterns). Each pattern is documented formally with accompanying UML diagrams. I'll introduce four of these patterns in this article.
Pattern #1: Composite View Pattern Today's Web applications typically have complex screens with repeated elements such as menus, navigation bars, company logos, and so on. The Composite View pattern suggests that you divide the screens into pieces, coding the pieces as self-contained units. This pattern helps your Web application's screen to have a common look and feel, and it promotes reuse of view components. It's easy to implement the Composite View pattern using JSPs. There are two built-in mechanisms: the <%@ include> directive and the <jsp:include> action. Both allow you to construct a JSP from a number of smaller JSPs (see Listing 1). Note that there is a subtle difference between JSP directives and JSP actions. Directives are acted upon when the JSP is translated to a servlet. This normally occurs just once during the lifetime of a Web application. JSP actions, however, are carried out at request time. Request-time inclusion gives you more flexibility and is generally the preferred approach. A more sophisticated implementation of the Composite View pattern allows you to generate all of an application's screens dynamically, based on settings in an XML-based configuration file. One JSP can be used as a template that specifies the common overall layout and defines subregions that are filled in at run time. Some of these regions contain common screen elements, while other regions show content that varies according to the user's current activity. You will find this approach in Sun's demonstration J2EE application, the Java Pet Store (see http://java.sun.com/blueprints/patterns/ CompositeView.html). Custom JSP tags are used to implement the Composite View pattern in the Pet Store application. Custom tags allow you to extend the JSP language and create your own tags. Each tag is associated with a Java class called the tag handler. Sun provides the JSP Standard Tag Library (JSTL), and BEA provides a set of custom tags that are packaged in a file called weblogic-tags.jar. This JAR file contains the tag handler classes and a tag library descriptor (TLD). To use these tags, copy this JAR file to the Web application that contains your JSPs and reference the tag library in the JSPs.
Pattern #2: Intercepting Filter Pattern Servlet filters, introduced with servlet specification version 2.3, are perfect for this pattern. A servlet filter is a small class that allows you to manipulate the request and/or response. A filter object is instantiated and managed by the servlet container, and has a life cycle similar to a servlet. Common uses for filters include logging HTTP requests, validating request parameters, and authorizing or denying access to resources. A simple logging filter is shown in Listing 2. Filters can also be used to modify the response, changing its format and/or contents. Filters are applied selectively to one or more servlets or JSPs by settings in web.xml (see Listing 3). Filters and filter mappings can also be configured in the WebLogic Administration Console by selecting your Web application and then clicking on the link labeled "Edit Web Application Deployment Descriptors." Servlet filters provide a mechanism to encapsulate recurring tasks in reusable units, a goal shared by the Composite View and several other J2EE design patterns. Since filters are applied declaratively rather than programmatically, they can easily be enabled, disabled, or replaced, just like the attachments on my cordless drill.
Pattern #3: Session Facade Pattern A commonly used J2EE pattern for EJBs is called the Session Facade pattern. In this pattern, a stateless session bean is used as a facade to hide access to one or more entity beans. Remote clients interact only with the session bean and never directly with the entity beans (see Figure 1). The first important benefit of the Session Facade pattern is that the client has a simpler interface and doesn't need to know about the details of which entity beans and which methods to use. In my analogy, the session bean is like the general contractor and the entity beans are the subcontractors. The Session Facade pattern improves performance as only the calls between the client and the session bean go across the network, while calls from the session bean to the entity beans are local to the EJB container. Performance can be further enhanced through the use of local interfaces, introduced as part of the EJB specification version 2.0. Local interfaces provide support for "lightweight" access from within the EJB container, avoiding the overhead associated with a remote interface. A third benefit of the Session Facade pattern relates to transactions. With container-managed transactions, the container begins a transaction when a method starts to execute and ends the transaction when the method returns. By enclosing the entity beans calls in the session bean method, the database operations are automatically grouped together as a transactional unit. To ensure that they participate in the same transaction, the entity bean methods should be assigned the "Required" transaction attribute in ejb-jar.xml (or by using the WebLogic Administration Console).
Pattern #4: Service Locator Pattern Most J2EE applications can take advantage of the Service Locator pattern. The idea behind this pattern is to provide a utility class that centralizes and hides all the details of a complex service. JNDI lookups are perfect candidates, since they generally need to be done only once, and the results can be saved for later use. Enterprise applications use JNDI lookups to locate EJB home interfaces, JDBC datasources, and Java Message Service (JMS) components. Listing 4 shows a simple implementation of the Service Locator pattern. Note that the class does not offer a public constructor, but instead has a public static method that allows callers to access a unique instance of the class. This demonstrates a basic GOF pattern, the Singleton pattern.
Conclusion BEA WEBLOGIC LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||