Best Lead In to Explain NetBeans FilterNode

I'm working through some edits for the "Node and Explorer View" chapter of the upcoming NetBeans Platform 6.9 book by Jurgen Petri. I quickly want to share why I think this book will be so great, by means of a text I've just been working on, which is (to my mind) the best lead in to a discussion on org.openide.nodes.FilterNode that I have ever read:

"There may be situations where you are not really satisfied by an existing node hierarchy. You may want to construct a new node hierarchy, based on the original nodes, but with different behavior. For example, you may want to provide different icons or filter out child nodes, while wanting to keep the rest of the behavior of the original node.

Initially, you might attempt to expand the existing node, or the node's children, overriding the relevant methods to obtain the required behavior. In the first place, this approach would be cumbersome and inflexible, given that a flexible alternative exists. Secondly, however, this approach might not be possible in all cases. Think, for example, of nodes that are defined in modules over which you have no control.

The decorator pattern, familiar from other areas of Java, provides a useful approach, letting you create a filter for a node that does not quite meet your requirements. In dealing with Java streams, for example, you have a BufferedInputStream, which is an InputStream that owns another InputStream, the first InputStream delegating the actual reading to the second InputStream. The first InputStream implements additional behavior, namely the buffering of the data, reducing the number of real read activities. Thus, the additional behavior is added to the decorated InputStream at runtime, dynamically. Its behavior is changed, without requiring a subclass. In the same way, you can decorate existing nodes and children to change their behavior."

The whole book is full of helpful explanations such as the above. Pretty cool, isn't it!


Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« April 2015