Navigator pattern

Printer-friendly versionPrinter-friendly version
Double dispatch pattern

 

 

Usually, the navigator interface provides an overloaded operation not just for the concrete types of the navigables, but also for all of their generalizations up to the navigable interface itself. A navigator operation which takes a concrete type as argument will then (in its implementation) forward first to the same operation taking the direct generalization as argument, before any other instructions. Consequently, the navigator interface reflects the type hierarchy of the navigables. This way a concrete navigator can apply behavior to a generalization, means to all objects of that type, in the same manner as with a concrete type.

The closest relative of the Navigator pattern is the Visitor pattern. The Visitor pattern applies behavior to object graphs by a double dispatch as well, but cannot determine its traversal, requires more implementation code for each visitable node, does not visit before and after visiting the children, the client passes the visitor to the visitable node and hence cannot implement a generalized behavior at that point, etc.

 

 

Simplified Navigator pattern

 

The following UML class diagram shows a simplified variant of the Navigator pattern:

 

Navigator pattern - simple - Class diagram.png
(click on the image)

 

 

Basic Navigator pattern

The following UML class diagram shows a basic variant of the Navigator pattern, where a "Navigator" interface allows for more than just one abstract navigator type, e.g. to segregate subdomains, but requires a type check on the navigables side:

 

Navigator pattern - basic - Class diagram.png
(click on the image)

 

 

Pages