Visitor

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(Description)
(Description)
Line 3: Line 3:
 
The Visitor is a [[Behavioural pattern|behavioural pattern]]. It allows the designer to define new operations on an object structure and its elements, without modifying the object structure itself.
 
The Visitor is a [[Behavioural pattern|behavioural pattern]]. It allows the designer to define new operations on an object structure and its elements, without modifying the object structure itself.
  
==Description==
+
==Structure==
 +
 
 +
The Visitor describes the relations between three distinct classes:
 +
{| cellpadding="5" cellspacing="0"
 +
|-
 +
||'''Class''' || '''Description'''
 +
|-
 +
|| ObjectStructure || Provides a visitor with access to its elements
 +
|-
 +
|| Element || Accepts visits.
 +
|-
 +
|| Visitor || Accesses an element to perform operations.
 +
|-
 +
|}
  
The Visitor describes three distinct components:
 
*Object Structure - Provides a visitor with access to its elements.
 
*Element - Accepts visits.
 
*Visitor - Accesses an element to perform operations.
 
 
Figure 1 graphically describes these Visitor pattern relationships.
 
Figure 1 graphically describes these Visitor pattern relationships.
[[image:Visitor.JPG|frame|centre|'''Figure 1: A [[UML]] [[Class diagram|class diagram]] representing the Visitor structure''']]
+
[[image:Visitor.JPG|frame|centre|'''Figure 1: A [[UML 2.1]] [[Class diagram|class diagram]] describing the Visitor structure''']]
 +
 
 +
==Communication==
 +
 
 +
The Visitor pattern allows a visitor object to access and perform operations on an element object. A summary of the Visitor pattern communications is given in Fig. 2. Here the following interactions are shown:
 +
{| cellpadding="5" cellspacing="0"
 +
 
 +
|-
 +
||'''Sequence''' ||'''Description'''
 +
|-
 +
|| 1 || The object aVisitor1 performs an operation on anElementA
 +
|-
 +
|| 2 || The object aVisitor2 performs an operation on anElementB
 +
|-
 +
|| 3 || The object aVisitor1 performs an operation on anElementA
 +
|-
 +
|| 4 || The object aVisitor2 performs an operation on anElementB
 +
|-
 +
|}
 +
 
 +
[[image:VisitorCommunications.JPG|frame|centre|'''Figure 2: A [[UML 2.1]] [[Communication diagram|communication diagram]] describing  the interactions between objects in the Visitor pattern''']]

Revision as of 10:03, 1 August 2008

Introduction

The Visitor is a behavioural pattern. It allows the designer to define new operations on an object structure and its elements, without modifying the object structure itself.

Structure

The Visitor describes the relations between three distinct classes:

Class Description
ObjectStructure Provides a visitor with access to its elements
Element Accepts visits.
Visitor Accesses an element to perform operations.

Figure 1 graphically describes these Visitor pattern relationships.

Figure 1: A UML 2.1 class diagram describing the Visitor structure

Communication

The Visitor pattern allows a visitor object to access and perform operations on an element object. A summary of the Visitor pattern communications is given in Fig. 2. Here the following interactions are shown:

Sequence Description
1 The object aVisitor1 performs an operation on anElementA
2 The object aVisitor2 performs an operation on anElementB
3 The object aVisitor1 performs an operation on anElementA
4 The object aVisitor2 performs an operation on anElementB
Figure 2: A UML 2.1 communication diagram describing the interactions between objects in the Visitor pattern
Personal tools