C # Design mode: Interpreter mode (Interpreter Pattern)

One, C# design pattern: Interpreter Pattern

1, the application of interpreter pattern is the difficulty in the application of Interpreter pattern, only to meet the “frequent changes in business rules, And similar patterns appear repeatedly, and it is easy to abstract as a problem of grammatical rules” to use the interpreter pattern
2, each interpreted class of the interpreter design pattern has its own rules and does not conflict with other business rules< /p>

Two, the following code

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _23. Interpreter mode
{
///
/// The application of Interpreter mode is the difficult point in the application of Interpreter mode. Only satisfying "Business rules change frequently, and similar patterns continue to reappear, and are easily abstracted into grammatical rules." It is suitable to use the Interpreter pattern.
/// 1. When a language needs to be interpreted and executed, it can When expressing sentences in the language as an abstract syntax tree, you can consider using the interpreter mode (such as XML document interpretation, regular expressions, etc.)
/// 2. You can use one for some recurring problems Simple language to express.
/// 3. The grammar of a language is relatively simple.
/// 4. When execution efficiency is not the key and the main concern Consider the interpreter mode when you have problems. (Note: High-efficiency interpreters are usually not realized by directly interpreting abstract syntax trees, but they need to be converted into other forms. The execution efficiency of using the interpreter mode is not high.)
///

class Program
{
static void Main(string[] args)
{
Context context
= new Context("usachi");
List
interpreterList = new List()
{
new Chinese(),
new Usa(),
};
foreach (var item in interpreterList)
{
item.Conversion(context);
}
Console.WriteLine(context.Get());
}
}

///
/// Context
///

public class Context
{
private string _Word = null;
public Context(string word)
{
this._Word = word;
}

public void Set(string newWord)
{
this._Word = newWord;
}

public string Get()
{
return this._Word;
}
}

///
/// Abstract interpreter
///

public abstract class PeopleInterpreter
{
public abstract void Conversion(Context context);
}
///
/// Chinese business
///

public class Chinese: PeopleInterpreter
{
private static Dictionary<char, string> _Dictionary = new Dictionary <char, string>( );
///
/// The Chinese explain the rules themselves
///

static Chinese()
{
_Dictionary.Add(
'u', "Americans< span style="color: #800000;">");
_Dictionary.Add(
's', "Use a knife and fork ");
_Dictionary.Add(
'a', "Eating,< span style="color: #800000;">");
}

public override void Conversion(Context context)
{
Console.WriteLine(
this.GetType().ToString() + "Business");
string text = context.Get();
if (string.IsNullOrEmpty(text))
return;
List
<string> numberList = new List<< span style="color: #0000ff;">string>();
foreach (var item in text.ToLower().ToArray())
{
if (_Dictionary.ContainsKey(item))
{
numberList.Add(_Dictionary[item]);
}
else
{
numberList.Add(item.ToString());
}
}
context.Set(
string.Concat(numberList));
}
}
///
/// American business
///

public class Usa: PeopleInterpreter
{
private static Dictionary<char, string> _Dictionary = new Dictionary <char, string>( );
///
/// Americans explain the rules themselves
///

static Usa()
{
_Dictionary.Add(
'c', "Chinese< span style="color: #800000;">");
_Dictionary.Add(
'h', "Use");
_Dictionary.Add(
'i', "Chopsticks to eat< span style="color: #800000;">");
}

public override void Conversion(Context context)
{
Console.WriteLine(
this.GetType().ToString() + "Business");
string text = context.Get();
if (string.IsNullOrEmpty(text))
return;
List
<string> numberList = new List<< span style="color: #0000ff;">string>();
foreach (var item in text.ToLower().ToArray())
{
if (_Dictionary.ContainsKey(item))
{
numberList.Add(_Dictionary[item]);
}
else
{
numberList.Add(item.ToString());
}
}
context.Set(
string.Concat(numberList));
}
}
}

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _23. Interpreter mode
{
///
/// The application of Interpreter mode is the difficult point in the application of Interpreter mode. Only satisfying "Business rules change frequently, and similar patterns continue to reappear, and are easily abstracted into grammatical rules." It is suitable to use the Interpreter pattern.
/// 1. When a language needs to be interpreted and executed, it can When expressing sentences in the language as an abstract syntax tree, you can consider using the interpreter mode (such as XML document interpretation, regular expressions, etc.)
/// 2. You can use one for some recurring problems Simple language to express.
/// 3. The grammar of a language is relatively simple.
/// 4. When execution efficiency is not the key and the main concern Consider the interpreter mode when you have problems. (Note: High-efficiency interpreters are usually not realized by directly interpreting abstract syntax trees, but they need to be converted into other forms. The execution efficiency of using the interpreter mode is not high.)
///

class Program
{
static void Main(string[] args)
{
Context context
= new Context("usachi");
List
interpreterList = new List()
{
new Chinese(),
new Usa(),
};
foreach (var item in interpreterList)
{
item.Conversion(context);
}
Console.WriteLine(context.Get());
}
}

///
/// Context
///

public class Context
{
private string _Word = null;
public Context(string word)
{
this._Word = word;
}

public void Set(string newWord)
{
this._Word = newWord;
}

public string Get()
{
return this._Word;
}
}

///
/// Abstract interpreter
///

public abstract class PeopleInterpreter
{
public abstract void Conversion(Context context);
}
///
/// Chinese business
///

public class Chinese: PeopleInterpreter
{
private static Dictionary<char, string> _Dictionary = new Dictionary <char, string>( );
///
/// The Chinese explain the rules themselves
///

static Chinese()
{
_Dictionary.Add(
'u', "Americans< span style="color: #800000;">");
_Dictionary.Add(
's', "Use a knife and fork ");
_Dictionary.Add(
'a', "Eating,< span style="color: #800000;">");
}

public override void Conversion(Context context)
{
Console.WriteLine(
this.GetType().ToString() + "Business");
string text = context.Get();
if (string.IsNullOrEmpty(text))
return;
List
<string> numberList = new List<< span style="color: #0000ff;">string>();
foreach (var item in text.ToLower().ToArray())
{
if (_Dictionary.ContainsKey(item))
{
numberList.Add(_Dictionary[item]);
}
else
{
numberList.Add(item.ToString());
}
}
context.Set(
string.Concat(numberList));
}
}
///
/// American business
///

public class Usa: PeopleInterpreter
{
private static Dictionary<char, string> _Dictionary = new Dictionary <char, string>( );
///
/// Americans explain the rules themselves
///

static Usa()
{
_Dictionary.Add(
'c', "Chinese< span style="color: #800000;">");
_Dictionary.Add(
'h', "Use");
_Dictionary.Add(
'i', "Chopsticks to eat< span style="color: #800000;">");
}

public override void Conversion(Context context)
{
Console.WriteLine(
this.GetType().ToString() + "Business");
string text = context.Get();
if (string.IsNullOrEmpty(text))
return;
List
<string> numberList = new List<< span style="color: #0000ff;">string>();
foreach (var item in text.ToLower().ToArray())
{
if (_Dictionary.ContainsKey(item))
{
numberList.Add(_Dictionary[item]);
}
else
{
numberList.Add(item.ToString());
}
}
context.Set(
string.Concat(numberList));
}
}
}

Leave a Comment

Your email address will not be published.