Interpreter Pattern Example Code—Course Rules

See the following code for an example of the Interpreter Pattern:

using System; // Interpreter Pattern Example Judith Bishop Oct 2007 // Sets up an object structure and interprets it with given data static class ElementExtensions { public static string gap; public static void Print(this Element element) { Console.WriteLine(gap+element + " " + element.Weight); if (element.Part!=null) { gap+=" "; Print(element.Part.Next); gap = gap.Substring(2); } if (element.Next!=null) Print(element.Next); } public static int Lab {get; set;} public static int Test {get; set;} public static void Summarize(this Element element) { if (element is Lab) Lab += element.Weight; else if (element is Test) Test += element.Weight; else if ((element is Midterm || element is Exam) && element.Part==null) Test += element.Weight; if (element.Part!=null) Summarize(element.Part.Next); if (element.Next!=null) Summarize(element.Next); } public static int [] values; public static int n; public static Context context; public static void SetUp (this Element element, Context c, int[] v) { context = c; context.Output=0; values = v; n = 0; } public static void Interpreter(this Element element) { if (element is Lab || element is Test) { context.Output += values[n]*element.Weight; n++; } else if ((element is Midterm || element is Exam) && element.Part==null) { context.Output += values[n]*element.Weight; n++; } if (element.Part!=null) Interpreter(element.Part.Next); if (element.Next!=null) ...

Get C# 3.0 Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.