O'Reilly logo

Java and XML, Second Edition by Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Wrappers and Decorators

One of the most common requests that comes up about JDOM is related to interfaces. Many, many users have asked for interfaces in JDOM, and that request has been consistently denied. The reasoning is simple: no set of common methods could be arrived at for all JDOM constructs. There has been a reluctance to use the DOM approach, which provides a set of common methods for most constructs. For example, getChildren( ) is on the common DOM org.w3c.dom.Node interface; however, it returns null when it doesn’t apply, such as to a Text node. The JDOM approach has been to only provide methods on a basic interface common to all JDOM classes, and no methods fulfilling this requirement have been found. Additionally, for every request to add interfaces, there has been a request to leave the API as is.

However, there are patterns that allow interface-type functionality to be used with JDOM without changing the API drastically (in fact, without changing it at all!). In this section, I want to talk about the most effective of those patterns, which involves using wrappers or decorators . I’m not going to dive into a lot of design pattern material in this book, but suffice it to say that a wrapper or decorator (I use the two interchangeably in this chapter) is on the exterior of existing classes, rather than on the interior, as a core JDOM interface would be. In other words, existing behavior is wrapped. In this section, I show you how this pattern allows you to customize ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required