Interpreter Pattern Code Review
Use the Interpreter Pattern
Code Review
- Interpreter Pattern Code Review: Define the Element Base Class and Associated Child Clases
- Interpreter Pattern Code Review: Define the Pattern Context Object
- Interpreter Pattern Code Review: Use the Interpreter Pattern
Code Walkthrough
The Program class creates an instance of the Context and Course classes.
static class IntArrayExtension
{
public static string Display(this int[] a)
{
string s = "[";
foreach (int i in a)
s += i + ", ";
return s.Substring(0, s.Length - 2) + "]";
}
}
class Program
{
static void Main(string[] args)
{
String rules = "COS333|L2|L2|L2|L2|L2|M25|(L40|T60|)|L10|E55|(L28|T73|)";
Int32[][] values = new[] { new [] {80,0,100,100,85,51,52,50,57,56}, new [] {87,95,100,100,77,70,99,100,75,94}, new [] {0,55,100,65,55,75,73,74,71,72} }; Console.WriteLine("{0}", rules);
Context context = new Context(rules);
Element course = new Course(context); course.Parse(context);
Console.WriteLine("\nCourse Structure:"); course.Print(); course.Summarize();
Console.WriteLine("\nSumming the weights\nLabs {0}% and Tests {1}%", Element.SumLab, Element.SumTest);
Console.WriteLine("\nInterpreter"); foreach (Int32[] student in values) { Console.Write(student.Display()); course.OutputResults(context, student); Console.WriteLine(" = {0}", context.Output / 100); } Console.Read(); } }
Int32[][] values = new[] { new [] {80,0,100,100,85,51,52,50,57,56}, new [] {87,95,100,100,77,70,99,100,75,94}, new [] {0,55,100,65,55,75,73,74,71,72} }; Console.WriteLine("{0}", rules);
Context context = new Context(rules);
Element course = new Course(context); course.Parse(context);
Console.WriteLine("\nCourse Structure:"); course.Print(); course.Summarize();
Console.WriteLine("\nSumming the weights\nLabs {0}% and Tests {1}%", Element.SumLab, Element.SumTest);
Console.WriteLine("\nInterpreter"); foreach (Int32[] student in values) { Console.Write(student.Display()); course.OutputResults(context, student); Console.WriteLine(" = {0}", context.Output / 100); } Console.Read(); } }
The Context class is updated with predefined grammatical input and passed to the Course class for parsing. Once the Context data has been parsed, the data is "interpreted" and results output.
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!