Building on Composite (Design Pattern), we want to add flexible operations to the data structure. We can implement the visitor pattern where the method to call is decided by looking at the run-time class of Element and at the run-time class of Visitor. Client can still call the method without knowing the sub-type of Element.

Benefits:

  • Easily add new operations to an existing class hierarchy without changing the classes themselves.
  • Particularly good when the classes come from a library, so cannot be changed.
  • Keep all functionality for one purpose in one place.
  • Rather than scattered across the structural classes separate structure and behaviour.

Disadvantages:

  • Separates structure and behaviour.
  • Adding new class means finding and changing all visitors.