解譯器模式分析、結構圖及基本代碼,解譯器模式結構圖
定義:給定一個語言,定義它的文法的一種表示,並定義一個解譯器,這個解譯器使用該表示來解釋語言中的句子。
適用地:當有一個語言需要解釋執行,並且你可將該語言中的句子表示為一個抽象文法樹時,可使用解譯器模式。如果一種特定類型的問題發生的頻率足夠高,那麼可能就值得將該問題的各個執行個體表述為一個簡單語言中的句子。這樣就可以構建一個解譯器,該解譯器通過解釋這些句子來解決該問題。像Regex、瀏覽器應用等等。
優點是可以很容易地擴充和改變文法,因為該模式使用類來表示文法規則,你可使用繼承來改變或擴充該文法。也比較容易實現文法,因為定義抽象文法樹中各個節點的類的實現大體相似,這些都易於直接編寫。
缺點是解譯器為文法中的每一種規則至少定義了一個類,因此包含許多規則的文法可能難以管理和維護。建議當文法非常複雜時,使用其他的技術如文法剖析器或編譯器產生器來處理。
結構圖:
基本代碼:
using System;
using System.Collections.Generic;
using System.Text;
namespace 解譯器模式
{
class Program
{
static void Main(string[] args)
{
Context context = new Context();
IList<AbstractExpression> list = new List<AbstractExpression>();
list.Add(new TerminalExpression());
list.Add(new NonterminalExpression());
list.Add(new TerminalExpression());
list.Add(new TerminalExpression());
foreach (AbstractExpression exp in list)
{
exp.Interpret(context);
}
Console.Read();
}
}
class Context
{
private string input;
public string Input
{
get { return input; }
set { input = value; }
}
private string output;
public string Output
{
get { return output; }
set { output = value; }
}
}
abstract class AbstractExpression
{
public abstract void Interpret(Context context);
}
class TerminalExpression : AbstractExpression
{
public override void Interpret(Context context)
{
Console.WriteLine("終端解譯器");
}
}
class NonterminalExpression : AbstractExpression
{
public override void Interpret(Context context)
{
Console.WriteLine("非終端解譯器");
}
}
}
:java解譯器模式計算機原始碼
解譯器模式計算機原始碼?
計算機源碼好找,這個解譯器模式指什嗎?
設計模式之Interpreter Pattern解譯器模式的ppt
建立型:
1. 單件模式(Singleton Pattern)
2. 抽象工廠(Abstract Factory)
3. 建造者模式(Builder)
4. Factory 方法模式(Factory Method)
5. 原型模式(Prototype)
結構型:
6. 適配器模式(Adapter Pattern)
7. 橋接模式(Bridge Pattern)
8. 裝飾模式(Decorator Pattern)
9. 組合模式(Composite Pattern)
10. 面板模式(Facade Pattern)
11. 享元模式(Flyweight Pattern)
12. 代理模式(Proxy Pattern)
13. 模板方法(Template Method)
14. 命令模式(Command Pattern)
15. 迭代器模式(Iterator Pattern)
行為型:
16. 觀察者模式(Observer Pattern)
17. 解譯器模式(Interpreter Pattern)
18. 中介者模式(Mediator Pattern)
19. 職責鏈模式(Chain of Responsibility Pattern)
20. 備忘錄模式(Memento Pattern)
21. 策略模式(Strategy Pattern)
22. 訪問者模式(Visitor Pattern)
23. 狀態模式(State Pattern)