Created
May 10, 2017 14:41
-
-
Save miteshsureja/6cc86c51ae3eb75f7ad75aa5246f7a95 to your computer and use it in GitHub Desktop.
Interpreter Design Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Context class | |
public class Context | |
{ | |
public int Input { get; set; } | |
public Context(int inputValue) | |
{ | |
Input = inputValue; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Expression Interface | |
public interface IExpression | |
{ | |
void Interpret(Context context); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Context ctx = new Context(5642); | |
IExpression exp1 = new WordExpression(); | |
exp1.Interpret(ctx); | |
Context ctx1 = new Context(397); | |
SubExpression subExp = new SubExpression(); | |
subExp.Expression1 = new WordExpression(); | |
subExp.Interpret(ctx1); | |
Console.ReadLine(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Non-terminal expression class | |
public class SubExpression : IExpression | |
{ | |
public IExpression Expression1 { get; set; } | |
public void Interpret(Context context) | |
{ | |
Console.WriteLine("Running Sub Expression 1"); | |
Expression1.Interpret(context); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Terminal expression class | |
public class WordExpression : IExpression | |
{ | |
public Dictionary<int, string> numberWord = new Dictionary<int, string>() { | |
{ 1, "One" }, { 2, "Two" }, { 3, "Three" }, { 4, "Four" }, { 5, "Five" }, | |
{ 6, "Six" }, { 7, "Seven" }, { 8, "Eight" }, { 9, "Nine" }, { 0, "Zero" } }; | |
public void Interpret(Context context) | |
{ | |
Console.Write("Input Value {0} - ", context.Input); | |
string value = context.Input.ToString(); | |
if (value.Length > 0) | |
{ | |
foreach (char chr in value) | |
{ | |
int number = (int)Char.GetNumericValue(chr); | |
Console.Write("{0} ", numberWord[number]); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interpreter Design Pattern