Interpreter Pattern Example Code—Mirrors

See the following code for another example of the Intrepreter Pattern:

using System; using System.Xml; using System.Reflection; using System.Collections; using System.Collections.Generic; using System.Windows.Forms; public class Mirror { // Mirrors Hans Lombard June 2006, revised Sept 2007 // Based on Views and Views-2 by Nigel Horspool, Judith Bishop, and D-J Miller // A general-purpose interpreter for any .NET API // Reads XML and executes the methods it represents // This example assumes the Windows Form API only in the final line // where Application.Run is called Stack objectStack; List<Command> commands; public object CurrentObject { get { return objectStack.Peek( ); } } public XmlTextReader Reader { get; set; } public object LastObject { get; set; } public Mirror(string spec) { objectStack = new Stack( ); objectStack.Push(null); // Register the commands commands = new List<Command>( ); commands.Add(new ElementCommand( )); commands.Add(new EndElementCommand( )); commands.Add(new AttributeCommand( )); Reader = new XmlTextReader(spec); while (Reader.Read( )) { InterpretCommands( ); bool b = Reader.IsEmptyElement; if (Reader.HasAttributes) { for (int i = 0; i < Reader.AttributeCount; i++) { Reader.MoveToAttribute(i); InterpretCommands( ); } } if (b) Pop( ); } } public void InterpretCommands( ) { // Run through the commands and interpret foreach (Command c in commands) c.Interpret(this); } public void Push(object o) { objectStack.Push(o); ...

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.