|
|
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
Pretend you're back in 1999 and you've just landed a job with a dot-com. Much of your compensation comes from stock options, and you are content. Fortunately, your work is interesting. You're developing an application builder that lets users visually construct Swing applications. So you seize upon a novel idea: display a view's component hierarchy next to the view itself like this:
Figure 1. A user interface (UI) builder prototype. Click on thumbnail to view full-size image.
The left panel shows the component hierarchy for the upper-right panel, and the lower-right panel contains a button that updates the tree. The idea, of course, is that users drag components into the upper-right panel and subsequently click the show component tree button to update the tree. In this simple prototype, you're only interested in getting the tree to reflect the upper-right panel's component hierarchy; you'll leave the drag and drop to someone else.
You propose this idea of exposing component trees to your colleagues, and they are enthusiastic. How long, they wonder, will it take to implement? The response is up to you, but as we're about to find out, the Adapter design pattern makes this an easy job.
Note: You can download this article's source code from Resources.
Adapters are necessary because dissimilar elements need to interoperate. From wrenches to computer networks, physical adapters
are abundant. In software, adapters make dissimilar software packages work together; for example, you might have a tree of
objects (call them Nodes) you want to display using Swing's JTree. The JTree class can't display your Nodes directly, but it can display TreeNode instances. With an adapter, you can map your Nodes to TreeNodes. Because Swing trees use the Adapter pattern, you can display any kind of tree—from Document Object Model (DOM) to Swing
component hierarchies to a compiler parse tree—just by implementing a simple adapter.
In Design Patterns, the authors describe the Adapter pattern like this:
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Figures 2 and 3 show the Adapter pattern's two standard variations.
Figure 2. Adapter with inheritance. Click on thumbnail to view full-size image.
Figure 3. Adapter with delegation. Click on thumbnail to view full-size image.
Adapters masquerade as one type of object by implementing its interface; they inherit (or delegate) functionality from another
class. That way, you can effectively substitute objects (known as Adaptees) for target (Target) objects. For the JTree in Figure 1, I adapt the objects in the tree (UIComponents) to Swing, so a JTree instance can manipulate them. Considering the amount of code that sits behind JTree, this simple adapter provides a huge return on investment. Let's see how it works.
First you need a fundamental understanding of Swing trees. So before I discuss Figure 1's adapter, I'll provide an overview.
Archived Discussions (Read only)