Interpreter解释器(行为型模式)
生活随笔
收集整理的這篇文章主要介紹了
Interpreter解释器(行为型模式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
動機(Motivation)
在軟件構建中,如果某一特定領域的問題比較復雜,類似的模式不斷重復出現,如果使用普通的變成方式來實現將面臨非常頻繁的變化。
在這種情況下,將特定領域的問題表達為某種語法規則情況下,然后構建一個解釋器(解釋器并不一樣)來解釋這樣的句子,從而達到解決問題的目的。
意圖(Intent) 給定一個語言,定義它的文法的一種表示,并定義一種解釋器,這個解釋器使用該表示來解釋語言中的句子。 ---《設計模式》GoF
結構(Structure)
{
????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;
????????}
????}
}
在這種情況下,將特定領域的問題表達為某種語法規則情況下,然后構建一個解釋器(解釋器并不一樣)來解釋這樣的句子,從而達到解決問題的目的。
意圖(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解释器(行为型模式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搜狗五笔与mac自带的五笔比较
- 下一篇: 时尚达人第一季:全新iPad轻松玩(全彩