日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Interpreter解释器(行为型模式)

發布時間:2024/3/26 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Interpreter解释器(行为型模式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
動機(Motivation) 在軟件構建中,如果某一特定領域的問題比較復雜,類似的模式不斷重復出現,如果使用普通的變成方式來實現將面臨非常頻繁的變化。
在這種情況下,將特定領域的問題表達為某種語法規則情況下,然后構建一個解釋器(解釋器并不一樣)來解釋這樣的句子,從而達到解決問題的目的。
意圖(Intent) 給定一個語言,定義它的文法的一種表示,并定義一種解釋器,這個解釋器使用該表示來解釋語言中的句子。 ---《設計模式》GoF
結構(Structure)

?

代碼 namespace?Interpreter
{
????
public?class?MainApp
????{
????????
static?void?Main()
????????{
????????????
string?romman?=?"六千四百五十二";?//轉換成6452,但是怎么解決無窮的位數?
????????}
????}

????
public?class?Context?//處理上下文類
????{
????????
public?Context(string?xx)
????????{
????????????Statement?
=?xx;
????????}
???????
public?string?Statement;
???????
public?int?Data;
???????
????}

????
public?abstract?class?Expression
????{
????????
protected?Dictionary<string,?int>?table?=?new?Dictionary<string,?int>(9);
????????
public?Expression()
????????{
????????????table.Add(
"",1);
????????????table.Add(
"",2);
????????????table.Add(
"",3);
????????????table.Add(
"",4);
????????????table.Add(
"",5);
????????????table.Add(
"",6);
????????????table.Add(
"",7);
????????????table.Add(
"",8);
????????????table.Add(
"",9);
????????}

????????
public?virtual?void?Interpret(Context?context)
????????{
????????????
if(context.Statement.Lenth?==?0)
????????????????
return;

????????????
foreach(string?key?in?table.Keys)
????????????{
????????????????
int?value?=?table[key];
????????????????
if(context.Statement.EndsWith(key?+?GetPostfix()))?//如果是以此關鍵字結尾比如400=四百
????????????????{
????????????????????context.Data?
+=?value?*?this.Multiplier();
????????????????????context.Statement?
=?context.Statement.Substring(0,context.Statement.Length?-?this.GetLength());
????????????????}

????????????????
if?(context.Statement.EndsWith(""))
????????????????{
????????????????????context.Statement?
=?context.Statement.Substring(0,?context.Statement.Length?-?this.GetLength());
????????????????}
????????????}
????????}

????????
public?abstract?string?GetPostfix();
????????
public?abstract?int?Multiplier();
????????
public?virtual?int?GetLength()
????????{
????????????
return?this.GetPostfix().Length?+?1;
????????}
????}

????
//
????public?class?GeExpression?:?Expression
????{
????????
public?override?string??GetPostfix()
????????{
????????????
return?"";
????????}?

????????
public?override?int??GetLength()
????????{
????????????
return?1;
????????}

????}
????
????
//十,?百,千處理方法同十
????public?class?ShiExpression?:?Expression
????{
????????
public?override?string??GetPostfix()
????????{
????????????
return?"";
????????}

????????
public?override?int??Multiplier()
????????{
????????????
return?10;
????????}
????}
????
public?class?BaiExpression?:?Expression{}
????
public?class?QianExpression?:?Expression{}
????
//萬,億等等處理類似
????public?class?WanExpression?:?Expression
????{
????????
public?override?string??GetPostfix()
????????{
????????????
return?"";
????????}

????????
public?override?void??Interpret(Context?context)
????????{
????????????
if(context.Statement.Length?==?0)
????????????????
return;
????????????ArrayList?tree?
=?new?ArrayList();
????????????tree.Add(
new?GeExpression());
????????????tree.Add(
new?ShiExpression());
????????????tree.Add(
new?WanExpression());
????????????tree.Add(
new?QianExpression());

????????????
foreach(string?key?in?table.Keys)
????????????{
????????????????
if(context.Statement.EndsWith(this.GetPostfix())
????????????????{
????????????????????
int?temp?=?context.Data;
????????????????????context.Data?
=?0;
????????????????????context.Statement?
=?context.Statement.Substring(0,context.Statement.Length?-?this.GetLength());

????????????????????
foreach(Expression?exp?in?tree)
????????????????????{
????????????????????????exp.Interpret(context);
????????????????????}

????????????????????context.Data?
=?temp?+?this.Multiplier()*context.Data;
????????????????}
????????????}
????????}

????????
public?override?int??Multiplier()
????????{
????????????
return?10000;
????????}
????}
}

?

?

?Interpreter模式的幾個要點

·Interpreter模式的應用場合是Interpreter模式應用中的難點,只有滿足"業務規則頻繁變化,且類似的模式不斷重復出現,并且容易抽象為語法規則的問題"才適合使用Interpreter模式。

·使用Interpreter模式來表示文法規則,從而可以使用面向對象技巧來方便地擴展文法。

·Interpreter模式比較適合簡單的文法表示,對于復雜的文法表示,Interpreter模式會產生比較大的類層次結構,需要求助于語法分析生成器這樣的標準工具。



http://www.cnblogs.com/jonniexie/articles/1641826.html

總結

以上是生活随笔為你收集整理的Interpreter解释器(行为型模式)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。