Unity はデザイン パターンを実装します - インタープリター モード
インタプリタパターンは規定の文法に従って解析するモードであり、実際のプロジェクトではほとんど使用されません。言語を指定して、その文法の表現を定義し、その表現を使用して言語の文を解釈するインタプリタを定義します。
以下に例を示します: ローマ字テキストを 10 進数形式に変換する
1.コンテキスト
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.表現
インタプリタ基本クラス
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.サウザンドエクスプレッション
千桁インタープリタ
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.百式
百桁通訳
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.テンエクスプレッション
10桁の通訳者
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.ワンエクスプレッション
一桁の通訳
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.テスト
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);
}
}