Programming - Visitor Behavioral Design Pattern


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.
The classes and/or objects participating in this exanmple are:


  • Visitor
    • 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 elements directly through its particular interface
  • ConcreteVisitor
    • implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class or 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.
  • Element
    and
    • defines an Accept operation that takes a visitor as an argument.
  • ConcreteElement
    • implements an Accept operation that takes a visitor as an argument
  • ObjectStructure
    • can enumerate its elements
    • may provide a high-level interface to allow the visitor to visit its elements
    • may either be a Composite (pattern) or a collection such as a list or a set
using
System;
using System.Collections;

namespace DoFactory.GangOfFour.Visitor.Structural
{

// MainApp test application

class MainApp
{
static void Main()
{
// Setup structure
ObjectStructure o = new ObjectStructure();
o.Attach(new ConcreteElementA());
o.Attach(new ConcreteElementB());

// Create visitor objects
ConcreteVisitor1 v1 = new ConcreteVisitor1();
ConcreteVisitor2 v2 = new ConcreteVisitor2();

// Structure accepting visitors
o.Accept(v1);
o.Accept(v2);

// Wait for user
Console.Read();
}
}

// "Visitor"

abstract class Visitor
{
public abstract void VisitConcreteElementA(
ConcreteElementA concreteElementA);
public abstract void VisitConcreteElementB(
ConcreteElementB concreteElementB);
}

// "ConcreteVisitor1"

class ConcreteVisitor1 : Visitor
{
public override void VisitConcreteElementA(
ConcreteElementA concreteElementA)
{
Console.WriteLine("{0} visited by {1}",
concreteElementA.GetType().Name, this.GetType().Name);
}

public override void VisitConcreteElementB(
ConcreteElementB concreteElementB)
{
Console.WriteLine("{0} visited by {1}",
concreteElementB.GetType().Name, this.GetType().Name);
}
}

// "ConcreteVisitor2"

class ConcreteVisitor2 : Visitor
{
public override void VisitConcreteElementA(
ConcreteElementA concreteElementA)
{
Console.WriteLine("{0} visited by {1}",
concreteElementA.GetType().Name, this.GetType().Name);
}

public override void VisitConcreteElementB(
ConcreteElementB concreteElementB)
{
Console.WriteLine("{0} visited by {1}",
concreteElementB.GetType().Name, this.GetType().Name);
}
}

// "Element"

abstract class Element
{
public abstract void Accept(Visitor visitor);
}

// "ConcreteElementA"

class ConcreteElementA : Element
{
public override void Accept(Visitor visitor)
{
visitor.VisitConcreteElementA(this);
}

public void OperationA()
{
}
}

// "ConcreteElementB"

class ConcreteElementB : Element
{
public override void Accept(Visitor visitor)
{
visitor.VisitConcreteElementB(this);
}

public void OperationB()
{
}
}

// "ObjectStructure"

class ObjectStructure
{
private ArrayList elements = new ArrayList();

public void Attach(Element element)
{
elements.Add(element);
}

public void Detach(Element element)
{
elements.Remove(element);
}

public void Accept(Visitor visitor)
{
foreach (Element e in elements)
{
e.Accept(visitor);
}
}
}
}

Output expected:


ConcreteElementA visited by ConcreteVisitor1
ConcreteElementB visited by ConcreteVisitor1
ConcreteElementA visited by ConcreteVisitor2
ConcreteElementB visited by ConcreteVisitor2


and
and

Home | Privacy Policy | Site Map | Links | Company | Contact Us | Arena           

We provide cutting edge software development, consultancy and web design services            

There are currently 646 user(s) with 40907 hits.