Unity實(shí)現(xiàn)設(shè)計(jì)模式——解釋器模式
解釋器模式(Interpreter Pattern)是一種按照規(guī)定語法進(jìn)行解析的模式,現(xiàn)實(shí)項(xiàng)目中用得較少。 給定一門語言,定義它的文法的一種表示,并定義一個(gè)解釋器,該解釋器使用該表示來解釋語言中的句子。
下面用一個(gè)例子演示:將羅馬文字轉(zhuǎn)換為十進(jìn)制的格式
1.Context
class Context
{
private string _input;
private int _output;
// Constructor
public Context(string input)
{
this._input = input;
}
// Gets or sets input
public string Input
{
get { return _input; }
set { _input = value; }
}
// Gets or sets output
public int Output
{
get { return _output; }
set { _output = value; }
}
}
2.Expression
解釋器基類
abstract class Expression
{
//"MCMXXVIII";
public void Interpret(Context context)
{
if (context.Input.Length == 0)
return;
if (context.Input.StartsWith(Nine()))
{
context.Output += (9 * Multiplier());
context.Input = context.Input.Substring(2);
}
else if (context.Input.StartsWith(Four()))
{
context.Output += (4 * Multiplier());
context.Input = context.Input.Substring(2);
}
else if (context.Input.StartsWith(Five()))
{
context.Output += (5 * Multiplier());
context.Input = context.Input.Substring(1);
}
while (context.Input.StartsWith(One()))
{
context.Output += (1 * Multiplier());
context.Input = context.Input.Substring(1);
}
}
public abstract string One();
public abstract string Four();
public abstract string Five();
public abstract string Nine();
public abstract int Multiplier();
}
3.ThousandExpression
千位數(shù)字解釋器
class ThousandExpression : Expression
{
public override string One() { return "M"; }
public override string Four() { return " "; }
public override string Five() { return " "; }
public override string Nine() { return " "; }
public override int Multiplier() { return 1000; }
}
4.HundredExpression
百位數(shù)字解釋器
class HundredExpression : Expression
{
public override string One() { return "C"; }
public override string Four() { return "CD"; }
public override string Five() { return "D"; }
public override string Nine() { return "CM"; }
public override int Multiplier() { return 100; }
}
5.TenExpression
十位數(shù)字解釋器文章來源:http://www.zghlxwxcb.cn/news/detail-729192.html
class TenExpression : Expression
{
public override string One() { return "X"; }
public override string Four() { return "XL"; }
public override string Five() { return "L"; }
public override string Nine() { return "XC"; }
public override int Multiplier() { return 10; }
}
6.OneExpression
個(gè)位數(shù)字解釋器文章來源地址http://www.zghlxwxcb.cn/news/detail-729192.html
class OneExpression : Expression
{
public override string One() { return "I"; }
public override string Four() { return "IV"; }
public override string Five() { return "V"; }
public override string Nine() { return "IX"; }
public override int Multiplier() { return 1; }
}
7.測(cè)試
public class InterpreterExample1 : MonoBehaviour
{
void Start()
{
string roman = "MCMXXVIII";
Context context = new Context(roman);
// Build the 'parse tree'
List<Expression> tree = new List<Expression>();
tree.Add(new ThousandExpression());
tree.Add(new HundredExpression());
tree.Add(new TenExpression());
tree.Add(new OneExpression());
// Interpret
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
Debug.Log(roman+" = "+ context.Output);
}
}
到了這里,關(guān)于Unity實(shí)現(xiàn)設(shè)計(jì)模式——解釋器模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!