Iterator Pattern Code Review
Use the Iterator Pattern
Code Review
- Iterator Pattern Code Review: Define the Main Tree Object
- Iterator Pattern Code Review: Define the Node and Person Objects
- Iterator Pattern Code Review: Use the Iterator Pattern
Code Walkthrough
The Program class creates the family tree structure and stores this in a variant variable, which will at runtime be converted to a Tree type.
class Program
{
static void Main(string[] args)
{
var family = new Tree(
new Node(new Person("Tom", 1950),
new Node(new Person("Peter", 1976),
new Node(new Person("Sarah", 2000), null,
new Node(new Person("James", 2002), null, null)),
new Node(new Person("Robert", 1978), null,
new Node(new Person("Mark", 1982),
new Node(new Person("Carrie", 2005), null, null),
null))),
null)
);
Console.WriteLine("Full Family"); foreach (Person p in family.Preorder) { Console.Write("{0} ", p); } Console.WriteLine("\n");
var selection = from p in family where p.Birth > 1980 orderby p.Name select p;
Console.WriteLine("Born after 1980 in alpha order"); foreach (Person p in selection) { Console.Write("{0} ", p); } Console.WriteLine("\n"); Console.Read(); } }
Console.WriteLine("Full Family"); foreach (Person p in family.Preorder) { Console.Write("{0} ", p); } Console.WriteLine("\n");
var selection = from p in family where p.Birth > 1980 orderby p.Name select p;
Console.WriteLine("Born after 1980 in alpha order"); foreach (Person p in selection) { Console.Write("{0} ", p); } Console.WriteLine("\n"); Console.Read(); } }
The code transverses through the Tree structure and displays all the nodes. Finally, using LINQ syntax the family tree is filtered by birth year and results displayed.
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!