|
|
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 3 of 5
The Swing Command pattern implementation closely adheres to the classic pattern's description in Design Patterns. In Swing, an invoker (in this case a menu item) maintains a reference to a command (in this case an action). That reference points to a command's abstract definition (the AbstractAction class), so that the command can represent any application-specific command (in this case a class that extends AbstractAction). When a trigger event occurs (in this case the menu item is selected), the invoker sends a request to the command and the
command implements its application-specific functionality.
Now that we've discussed a classic Command pattern implementation for client-side Java, let's take a look at another Command pattern implementation for server-side Java that deviates a bit from the classic Command pattern definition.
Struts, a popular open source JSP application framework from the Apache Software Foundation, implements numerous design patterns, including the Model-View-Controller (MVC) pattern and, of course, the Command pattern. By applying design patterns to server-side Java components, such as servlets and JSPs, Struts makes it simple to implement modular, easily maintainable, and extensible Web applications.
Struts comprises numerous pieces of distinct functionality, including JavaBean utilities, an XML digester, a database connection pool, and a comprehensive set of JSP custom tags. But the most significant cog in the Struts wheel is an MVC framework that revolves around the Command pattern. The rest of this article outlines that framework with a simple Web application.
All Web applications, at their core, perform the same basic function: field HTTP requests and respond to those requests by performing some application-specific functionality. Struts provides a servlet—known as the action servlet—that handles HTTP requests and ultimately invokes an application-specific action. The Struts action servlet turns HTTP requests into actions, but as a developer employing Struts to implement Web applications, you can remain blissfully ignorant of how it works—that's the beauty of application frameworks and the Command pattern. You simply need to know how to map an HTTP request to a Struts action, how to implement that action, and you're off and running. Figure 5 shows a Struts-actions class diagram and how those actions relate to the action servlet.
Figure 5. Struts actions class diagram. Click on thumbnail to view full-size image.
Struts actions typically extend the Action class. Struts also provides Action subclasses you can extend for special-needs actions. For example, you can extend the DispatchAction class for actions that combine a set of similar actions in a single action class. In this article, I examine a simple action
implementation that directly extends the Action class.
Every Struts action maintains a reference to the action servlet, which is handy for accessing various servlet properties;
for example, you can obtain a reference to your Web application with the action servlet's getServletContext() method. You can use that application to access application attributes, as you'll see shortly.