Context
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter
{
/**//// <summary>
/// Context
/// </summary>
public class Context
{
private string _input;
private string _output;
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="input">輸入內容</param>
public Context(string input)
{
this._input = input;
}
/**//// <summary>
/// 輸入內容
/// </summary>
public string Input
{
get { return _input; }
set { _input = value; }
}
/**//// <summary>
/// 輸出內容
/// </summary>
public string Output
{
get { return _output; }
set { _output = value; }
}
}
}
AbstractExpression
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Interpreter
{
/**//// <summary>
/// 抽象公式(AbstractExpression)
/// </summary>
public abstract class AbstractExpression
{
/**//// <summary>
/// 解釋Context的方法
/// </summary>
/// <param name="context">context</param>
public void Interpret(Context context)
{
if (String.IsNullOrEmpty(context.Input))
{
return;
}
context.Output += GetCSharp(context.Input);
}
/**//// <summary>
/// 獲得輸入內容所對應的C#代碼
/// </summary>
/// <param name="source">source</param>
/// <returns></returns>
private string GetCSharp(string source)
{
string csharp = "";
string Word = "";
// 從輸入內容中取得要解釋的詞
word = GetWord(source);
// 從字典中找到Word所對應的C#代碼
GetDictionary().TryGetValue(Word, out csharp);
return csharp;
}
/**//// <summary>
/// 從輸入內容中取得要解釋的詞
/// </summary>
/// <param name="source">source</param>
/// <returns></returns>
public abstract string GetWord(string source);
/**//// <summary>
/// 獲取字典
/// </summary>
/// <returns></returns>
public abstract Dictionary<string, string> GetDictionary();
}
}