I l@ve RuBoard Previous Section Next Section

Chapter 10. Visitor

This chapter discusses generic components that use the Visitor design pattern (Gamma et al. 1995). Visitor is a powerful—if controversial—design pattern that changes the dependency trade-offs involved in class design.

Visitor gives you a surprising amount of flexibility in a certain area: You can add virtual functions to a class hierarchy without recompiling them or their existing clients. However, this flexibility comes at the expense of disabling features that designers take for granted: You cannot add a new leaf class to the hierarchy without recompiling the hierarchy and all its clients. Therefore, Visitor's operational area is limited to very stable hierarchies (you seldom add new classes) and heavy processing needs (you often add new virtual functions).

Visitor goes against programmers' intuition; therefore, a careful implementation and rigorous discipline are essential to using it successfully. This chapter's goal is to craft a dependable generic implementation of Visitor that leaves as little burden on the application programmer as possible.

After reading this chapter, you will

  • Understand how Visitor works

  • Know when to apply the Visitor pattern and, equally important, when not to

  • Understand the basic implementation of a visitor (the GoF implementation)

  • Know how to overcome some drawbacks of the GoF Visitor implementation

  • Learn how most of the decisions that pertain to implementing Visitor can be moved up to a library

  • Be armed with powerful generic components that will help you greatly in implementing visitors that specifically solve your problems

    I l@ve RuBoard Previous Section Next Section