﻿<?xml-stylesheet type="text/xsl" href="../../templates/doc.xsl"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html 
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:doc="http://www.lepus.org.uk/doc" 
	xmlns:classz="http://www.lepus.org.uk/classz" 
	xmlns:fopl="http://www.lepus.org.uk/fopl" 
	xml:lang="EN-GB" 
	xmlns:v="urn:schemas-microsoft-com:vml" 
	xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
	<link rel="stylesheet" type="text/css" href="../../templates/doc.css" />
	<title>Visitor pattern: The LePUS3 &amp; Class-Z companion to the GoF</title>
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta name="Author" content="Amnon H Eden" />
</head>


<body>


<h5><a href="./">The 'Gang of Four' Companion:</a></h5>

<p class="pagetitle">Visitor Design Pattern</p>

<h5><a href="./">Formal specification of design patterns in LePUS3 and Class-Z</a></h5>

<p><a href="companion.pdf" style="border:0px">
	<img alt="Print this document" src="../../site/print.gif" /></a></p>

<div class="abstract">

	<p>This page is part of the <a href="./">The 'Gang of Four' Companion</a> 
	dedicated to the formal specification in LePUS3 and Class-Z of patterns from the 'Gang of Four' catalogue [<a href="http://www.lepus.org.uk/ref/index.xml#bib">Gamma 
	et al 1995</a>].</p>

</div> <!-- Abstract-->

<doc:toc />

<h3>Links</h3>

<div class="abstract">

<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

	<p>Download:</p>
	
	<ul>
		<li><a href="../../spec/gof/lepus3/gof.vsd">The 'gang of four' patterns in LePUS3 (Visio 2003 format)</a></li>
		<li><a href="../../spec/gof/uml/gof.vsd">The 'gang of four' patterns in UML (Visio 2003 format)</a></li>
	</ul>
	
</div> <!-- Links -->


<h1>The Visitor design motif</h1>

<p>The informal description: Excerpts from [<a href="../index.xml#bib">Gamma et al. 1995</a>] (adapted for 
	this purpose):</p>

<p><strong>Intent</strong>: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.</p>
	
<p><strong>Structure</strong>: Original OMT diagram converted to UML (<a href="faq.xml#Why_did_you_convert_the_original_diagrams_to_UML_and_how?">Why 
	and How?</a>):</p>

<img alt="Visitor in UML" src="../../spec/gof/uml/Visitor.png" /> <br />

<p><strong>Participants</strong>: </p>
<ul>
	<li><b>Visitor</b>: declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the element directly through its particular interface.</li>
	<li><b>ConcreteVisitor</b>: implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.</li>
	<li><b>Element</b>: defines an Accept operation that takes a visitor as an argument.</li>
	<li><b>ConcreteElement</b>: implements an Accept operation that takes a visitor as an argument.</li>
	<li><b>ObjectStructure</b>:
		<ul>
			<li>Can enumerate its elements.</li>
			<li>May provide a high-level interface to allow the visitor to visit its elements.</li>
			<li>May either be a <a href="./Composite.xml">composite</a> or a collection such as a list or a set.</li>
		</ul>
	</li>
</ul>

<p><strong>Collaborations</strong>:</p>
<ul>
	<li>A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor.</li>
	<li>When an element is visited, it calls the Visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary.</li>
</ul>

<h1>Formal specification</h1>
<p>See also:</p>
<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="../../spec/gof/classz/Visitor.xml">Visitor in XML</a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

<table class="chartandschema">
<tr>
	<td>
	<img class="chart" id="Visitor" alt="Visitor in LePUS3" src="../../spec/gof/lepus3/Visitor.gif" /></td>
</tr>
<tr>
	<th>Visitor in LePUS3 (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><doc:include value="../../spec/gof/classz/Visitor.xml" /></td>
</tr>
<tr>
	<th>Visitor in Class-Z (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
</table>
</body>
</html>
