Visitor Pattern Code Review

Define the Element Interface and Handler Objects

Code Review

Code Walkthrough

Define the IElement abstract class to be used as the base class for all classes that can be visited.

abstract class IElement { public abstract void Accept(IVisitor visitor); }

This abstract class defines the Accept() method, which accepts the visitor object as a parameter.

The Element class (object) defines the base class for a type of class that can be visited and invokes the Visit() method on the visitor class (object).

class Element : IElement { public Int32 Weight { get; set; } public Element Next { get; set; } public Element Child { get; set; }
public override void Accept(IVisitor visitor) { visitor.Visit(this); } private Int32 GetNumber(Context context) { Int32 atSpace = context.Input.IndexOf(' '); Int32 number = Int32.Parse(context.Input.Substring(1, atSpace)); context.Input = context.Input.Substring(atSpace + 1); return (number); } public void Parse(Context context) { String starters = "LTME";
if ((context.Input.Length > 0) && (starters.IndexOf(context.Input[0]) >= 0)) { switch (context.Input[0]) { case 'L': this.Next = new Lab(); break;
case 'T': this.Next = new Test(); break;
case 'M': this.Next = new MidTerm(); break;
case 'E': this.Next = new Exam(); break;
default: break; } this.Next.Weight = this.GetNumber(context); if ((context.Input.Length > 0) && (context.Input[0] == '(')) { context.Input = context.Input.Substring(1); this.Next.Child = new Element(); this.Next.Child.Parse(context); Element e = this.Next.Child; while (e != null) { e.Weight = e.Weight * this.Next.Weight / 100; e = e.Next; } context.Input = context.Input.Substring(2); } this.Next.Parse(context); } } }

The Course class (object) inherits from the Element base class and defines one type of class that can be visited.

class Course : Element { public String Name { get; set; }
public Course(Context context) { this.Name = context.Input.Substring(0, 6); context.Input = context.Input.Substring(7); }
public override void Accept(IVisitor visitor) { visitor.Visit(this); } }

The Course class invokes the Visit() method on the visitor class (object).

The Lab class (object) inherits from the Element base class and defines another type of class that can be visited.

class Lab : Element { public override void Accept(IVisitor visitor) { visitor.Visit(this); } }

The Lab class invokes the Visit() method on the visitor class (object).

The Test class (object) inherits from the Element base class and defines another type of class that can be visited.

class Test : Element { public override void Accept(IVisitor visitor) { visitor.Visit(this); } }

The Test class invokes the Visit() method on the visitor class (object).

The MidTerm class (object) inherits from the Element base class and defines another type of class that can be visited.

class MidTerm : Element { public override void Accept(IVisitor visitor) { visitor.Visit(this); } }

The MidTerm class invokes the Visit() method on the visitor class (object).

The Exam class (object) inherits from the Element base class and defines another type of class that can be visited.

class Exam : Element { public override void Accept(IVisitor visitor) { visitor.Visit(this); } }

The Exam class invokes the Visit() method on the visitor class (object).