Interpreter – It provides to evaluate the language grammar and description. This idea bases on having the class for each symbol in a specialized computer language.
From Design Pattern – Behavioral Patterns
Structure
Applicability
Note: It is often used on the specialized database query language such as SQL, the technical computer language often used to describe communication protocols. The most general-purpose computer language incorporates several specialized languages.
Pros and Cons
PROS
CONS
How to Implement
#1 Basic Sample
static void Main(string[] args)
{
List<AbstractExpression> objExpression = new List<AbstractExpression>();
Context context = new Context(DateTime.Now);
Console.WriteLine("Please select the Expression : MM DD YYYY or YYYY MM DD or DD MM YYYY ");
context.expression = Console.ReadLine();
string[] strArray = context.expression.Split(' ');
foreach (var item in strArray)
{
if (item == "DD")
objExpression.Add(new DayExpression());
else if (item == "MM")
objExpression.Add(new MonthExpression());
else if (item == "YYYY")
objExpression.Add(new YearExpression());
}
objExpression.Add(new SeparatorExpression());
foreach (var obj in objExpression)
{
obj.Evaluate(context);
}
Console.WriteLine(context.expression);
Console.ReadKey();
}
public class Context
{
public string expression { get; set; }
public DateTime date { get; set; }
public Context(DateTime date) => this.date = date;
}
public interface AbstractExpression
{
void Evaluate(Context context);
}
public class DayExpression : AbstractExpression
{
private string expression;
public void Evaluate(Context context)
{
expression = context.expression;
context.expression = expression.Replace("DD", context.date.Day.ToString());
}
}
public class MonthExpression : AbstractExpression
{
private string expression;
public void Evaluate(Context context)
{
expression = context.expression;
context.expression = expression.Replace("MM", context.date.Month.ToString());
}
}
public class YearExpression : AbstractExpression
{
private string expression;
public void Evaluate(Context context)
{
expression = context.expression;
context.expression = expression.Replace("YYYY", context.date.Year.ToString());
}
}
public class SeparatorExpression : AbstractExpression
{
private string expression;
public void Evaluate(Context context)
{
expression = context.expression;
context.expression = expression.Replace(" ", "-");
}
} #2 Web Application Project
Step 1 Create the interpreter pattern interface class files and the interpreter pattern-related interface class files in the Core project. One interface class file name sets “IInterpreter.” Other interface class file name sets “IAbstractExpression.”
Step 2 Edit the interface class file from “IAbstractExpression” to start. Create the class files which need to inherit the “IAbstractExpression” interface class file.
Continued on next page…
Page: 1 2
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…
View Comments