Visitor Pattern Code Review
Define the Element Interface and Handler Objects
Code Review
- Visitor Pattern Code Review: Define the Pattern Interface and Handler Objects
- Visitor Pattern Code Review: Define the Element Interface and Handler Objects
- Visitor Pattern Code Review: Define the Context Object
- Visitor Pattern Code Review: Use the Visitor Pattern
Code Walkthrough
Define the IElement abstract class to be used as the base class for all classes that can be visited.
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).
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.
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.
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.
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.
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.
The Exam class invokes the Visit() method on the visitor class (object).
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!