思路话语

。Arlen:思想有多远你就能走多远...

设计模式之Interpreter

看uml图已经理解了大半,但是右边这个线不怎么明白。看代码就全明白了:

namespace Interpreter_DesignPattern
15{
16    using System;
17    using System.Collections;
18
19    class Context 
20    {
21        
22    }

23
24    abstract class AbstractExpression 
25    {
26        abstract public void Interpret(Context c);
27    }

28
29    // class for terminal symbol
30    class TerminalExpression : AbstractExpression
31    {
32        override public void Interpret(Context c)    
33        {
34            
35        }

36    }

37
38    // class for grammar rule (one per rule needed)
39    class NonterminalExpression : AbstractExpression
40    {
41        override public void Interpret(Context c)    
42        {
43            
44        }
    
45    }

46    // to extend grammar, just add other NonterminalExpression classes
47
48    /// <summary>
49    ///    Summary description for Client.
50    /// </summary>

51    public class Client
52    {
53        public static int Main(string[] args)
54        {
55            Context c = new Context();
56            ArrayList l = new ArrayList(); //really need a tree here!
57
58            // build up context information 
59            // . . .
60
61            // Populate abstract syntax tree with data
62            l.Add(new TerminalExpression());
63            l.Add(new NonterminalExpression());
64
65            // interpret
66            foreach (AbstractExpression exp in l)
67            {
68                exp.Interpret(c);
69            }

70                    
71            return 0;
72        }

73    }

74}

posted on 2010-01-14 22:15  Arlen  阅读(116)  评论(0)    收藏  举报

导航