In fact, you do not understand the boss's heart interpreter mode

27.1 In fact, you do not understand the boss's heart

27.2 Interpreter pattern

Given a language, which defines a grammar representation and define an interpreter, the interpreter uses the representation to interpret sentences in the language,

Interpreter pattern to be solved is that, if the frequency of a particular type of problem occurs is high enough, it may be worth each instance the problem is expressed as a simple sentences in the language, so that you can build an interpreter, the interpreter is to solve the problem by interpreting these sentences, for example, often search for a matching string of characters in a string or determine whether the format specified by us, what techniques usually use? Regular expressions,

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(Exp AbstractExpression in List) 
            { 
                exp.Interpret (context); 
            } 

            Console.Read (); 
        } 
    } 

    // abstract expression
     // declaration explaining the operation of an abstract, this interface is abstract syntax tree shared by all nodes , 
    abstract  class AbstractExpression 
    { 
        public  abstract  void the Interpret (the context context); 
    } 

    // terminator expression
     // achieve explaining the operation of the terminal symbols in the grammar associated
     // implementation of the interface required abstract expression, mainly Interpret is a method,
     // grammar each terminal symbol has a particular expression termination Correspondingly, 
    class TerminalExpression: AbstractExpression 
    { 
        public  the override void the Interpret (the Context context) 
        { 
            Console.WriteLine ( " Terminal Interpreter " ); 
        } 
    } 

    // nonterminal expressions
     // implemented in explaining the operation of the grammar nonterminals,
     // for each grammar rule R1 , R2, Rn requires a particular nonterminal expression class,
     // achieved by implementing Interpret explaining the operation of a method of abstract expressions,
     // explaining the operation recursively calls on behalf of the above mentioned R1, R2, Rn in each symbol instance variable, 
    class NonterminalExpression: AbstractExpression 
    { 
        public  the override  void the Interpret (the context context) 
        { 
            Console.WriteLine ( " non-end interpreter ); 
        }" 
    } 

    // contains global information other than the interpreter, 
    class the Context 
    { 
        Private  String INPUT; 

        public  String the Input 
        { 
            GET { return INPUT;}
             SET {INPUT = value;} 
        } 

        Private  String Output; 

        public  String the Output 
        { 
            GET { return Output;}
             SET {Output = value;} 
        } 
    } 

}
View Code

Benefits 27.3 interpreter mode

Usually when there is a need to explain the implementation language, and you may represent the language sentence into an abstract syntax tree, using the interpreter mode,

Interpreter pattern what good is it? With the interpreter mode, it means that you can easily change and expand the grammar, because this mode uses classes to represent grammar rules, you can use inheritance to change or expand the grammar, grammar is relatively easy to implement, because the definition of an abstract syntax tree implementing class of each node substantially similar, these classes are easy to be written directly,

Interpreter pattern is also insufficient to explain the mode grammar Each rule defines at least one class and therefore contains many rules of grammar can be difficult to manage and maintain, it is recommended when grammar is very complex, using other techniques such as parser or generating compiler to handle,

27.4 Music Interpreter

Music interpreter achieve 27.5

27.6 Liaoshirushen

Guess you like

Origin www.cnblogs.com/huangxuQaQ/p/11329141.html