The Interpreter Pattern of Design Patterns

Today, May 1st, I returned to Zhengzhou and stayed at home for three days. Played mobile games for a few days and spent a lot of money. The family is going to build another house, and I should also make some contributions to the family. Really fucking tired, over 30, single, and now a little afraid of marriage. I am free to make an accounting software in the near future, otherwise the money will be spent too fast. This month, I have to take the soft exam on the 20th. I should not be able to take the exam, so I basically gave up. The boss originally said that it would go up by 3K, but it turned into 2K, a pit, a routine.

Recently, the code I wrote has been upgraded to github and csdn code. This managed code platform is definitely a programmer's welfare.

Without further ado, let's talk about design patterns, and start talking about behavioral patterns today. I just saw that the C# version has been updated to 8.0, which made me realize that programming ideas are very important, otherwise it will be absolutely tiring to be a programmer. The mental thing is the inner strength, and the ordinary moves that the inner strength cock makes are also great. 13.

When writing code, don't deliberately use design patterns, it is fundamental to use programming ideas. Like the object-oriented language C#, we can start from two aspects when designing, abstract aspect = "concrete; core code =" encapsulation. The principle is easy to expand, easy to maintain, high cohesion and low coupling.

Types can be divided into two types in terms of quantity, representing the type of a single number and the type of multiple numbers.

Abstract, interface, all non-specific, can only be referenced, can not create objects. In object orientation all objects must have a host.

The four major artifacts in the programming world, compilers, interpreters, linkers, and debuggers. Interpret one form of expression into another form of expression.

Talking about it, go directly to the code. I'll explain.

//Host class that wraps properties

class PlayContext
{

private string text;
public string PlayText
{
get { return text; }
set { text = value; }
}
}

//The abstract concept is to explain the content.

abstract class Expression
{

public void Interpret(PlayContext context)
{
if (context.PlayText.Length == 0)
{
return;
}
else
{
string playKey = context.PlayText.Substring(0, 1);
context.PlayText = context.PlayText.Substring(2);
double playValue = Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" ")));
context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ") + 1);

Excute(playKey, playValue);

}
}
//Different specific explanation content
public abstract void Execute(string key, double value);
}

// specific explanation content

class Note : Expression
{
public override void Excute(string key, double value)
{
string note = "";
switch (key)
{
case "C":
note = "1";
break;
case "D":
note = "2";
break;
case "E":
note = "3";
break;
case "F":
note = "4";
break;
case "G":
note = "5";
break;
case "A":
note = "6";
break;
case "B":
note = "7";
break;

}
Console.Write("{0} ", note);
}
}

//different explanation content

class Scale : Expression
{
public override void Excute(string key, double value)
{
string scale = "";
switch (Convert.ToInt32(value))
{
case 1:
scale = "低音";
break;
case 2:
scale = "中音";
break;
case 3:
scale = "高音";
break;

}
Console.Write("{0} ", scale);
}
}

// explain the different situations

PlayContext context = new PlayContext();

Console.WriteLine("上海滩:");
//context.演奏文本 = "T 500 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 D 0.5 E 0.5 G 3 D 0.5 E 0.5 O 1 A 3 A 0.5 O 2 C 0.5 D 1.5 E 0.5 D 0.5 O 1 B 0.5 A 0.5 O 2 C 0.5 O 1 G 3 P 0.5 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 D 0.5 E 0.5 G 3 D 0.5 E 0.5 O 1 A 3 A 0.5 O 2 C 0.5 D 1.5 E 0.5 D 0.5 O 1 B 0.5 A 0.5 G 0.5 O 2 C 3 P 0.5 O 3 C 0.5 C 0.5 O 2 A 0.5 O 3 C 2 P 0.5 O 2 A 0.5 O 3 C 0.5 O 2 A 0.5 G 2.5 G 0.5 E 0.5 A 1.5 G 0.5 C 1 D 0.25 C 0.25 D 0.5 E 2.5 E 0.5 E 0.5 D 0.5 E 2.5 O 3 C 0.5 C 0.5 O 2 B 0.5 A 3 E 0.5 E 0.5 D 1.5 E 0.5 O 3 C 0.5 O 2 B 0.5 A 0.5 E 0.5 G 2 P 0.5 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 D 0.5 E 0.5 G 3 D 0.5 E 0.5 O 1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.25 D 3 E 0.5 G 0.5 O 3 C 1.5 O 2 B 0.25 O 3 C 0.25 O 2 B 1 A 0.5 G 0.5 A 0.5 O 3 C 0.5 O 2 E 0.5 D 0.5 C 1 C 0.5 C 0.5 C 0.5 O 3 C 1 O 2 G 0.25 A 0.25 G 0.5 D 0.25 E 0.25 D 0.5 C 0.5 C 3 ";
Expression expression = null;
try
{
while (context.PlayText.Length > 0)
{
string str = context.PlayText.Substring(0, 1);
switch (str)
{
case "O":
expression = new Scale();
break;
case "T":
expression = new Speed();
break;
case "C":
case "D":
case "E":
case "F":
case "G":
case "A":
case "B":
case "P":
expression = new Note();
break;

}

//Operate the same instance in different situations
expression.Interpret(context);

}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.Read();

 

Summary: Interpretation mode is just another interpretation of a representation. The purpose of using design patterns is to be easy to expand and maintain, and it is precisely because there is an inheritance and implementation mechanism that separates abstract and concrete from object-oriented.

The most precious thing for programmers is programming ideas. Let's not talk about it. Now my heart is a little restless. Let's stop here. Please bear with me.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325149634&siteId=291194637