JavaJava设计模式

设计模式-解释器模式

2019-05-09  本文已影响14人  smallmartial

1.定义

 解释器模式(Interpreter),给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。

2.概述

image.png

3.描述

image.png

3.实现

抽象表达式 Expression

public interface Node{
      public void parse(Context text);
      public void execute();
} 

Terminal Expression:终结符表达式_1

public class SubjectPronounOrNounNode implements Node{
      String [] word={"You","He","Teacher","Student"};
      String token; 
      boolean boo;
      public void parse(Context context){
             token=context.nextToken();
             int i=0;
             for(i=0;i<word.length;i++){
                if(token.equalsIgnoreCase(word[i])){
                    boo=true;
                    break;
                }
             }
             if(i==word.length)
                boo=false;
      }
      public void execute(){
            if(boo){
                if(token.equalsIgnoreCase(word[0]))
                    System.out.print("你"); 
                if(token.equalsIgnoreCase(word[1]))
                    System.out.print("他"); 
                if(token.equalsIgnoreCase(word[2]))
                   System.out.print("老师"); 
                if(token.equalsIgnoreCase(word[3]))
                    System.out.print("学生"); 
            }
            else{
                 System.out.print(token+"(不是该语言中的语句)");
           }        
     }
}

Terminal Expression:终结符表达式_2

public class ObjectPronounOrNounNode implements Node{
      String [] word={"Me","Him","Tiger","Apple"};
      String token; 
      boolean boo;
      public void parse(Context context){
             token=context.nextToken();
             int i=0;
             for(i=0;i<word.length;i++){
                if(token.equalsIgnoreCase(word[i])){
                    boo=true;
                    break;
                }
             }
             if(i==word.length)
                boo=false;
      }
      public void execute(){
            if(boo){
                if(token.equalsIgnoreCase(word[0]))
                    System.out.print("我"); 
                if(token.equalsIgnoreCase(word[1]))
                    System.out.print("他"); 
                if(token.equalsIgnoreCase(word[2]))
                   System.out.print("老虎"); 
                if(token.equalsIgnoreCase(word[3]))
                    System.out.print("苹果"); 
            }
            else{
                System.out.print(token+"(不是该语言中的语句)");
            }        
     }
}

Terminal Expression:终结符表达式_3

public class VerbNode implements Node{
      String [] word={"Drink","Eat","Look","beat"};
      String token; 
      boolean boo;
      public void parse(Context context){
             token=context.nextToken();
             int i=0;
             for(i=0;i<word.length;i++){
                if(token.equalsIgnoreCase(word[i])){
                    boo=true;
                    break;
                }
             }
             if(i==word.length)
                boo=false;
      }
      public void execute(){
            if(boo){
                if(token.equalsIgnoreCase(word[0]))
                    System.out.print("喝"); 
                if(token.equalsIgnoreCase(word[1]))
                    System.out.print("吃"); 
                if(token.equalsIgnoreCase(word[2]))
                   System.out.print("看"); 
                if(token.equalsIgnoreCase(word[3]))
                    System.out.print("打"); 
            }
            else{
               System.out.print(token+"(不是该语言中的语句)");
            }        
     }
} 

Nonterminal Expression:非终结符表达式_1

public class SentenceNode implements Node{
      Node subjectNode,predicateNode;
      public void parse(Context context){
            subjectNode =new SubjectNode();
            predicateNode=new PredicateNode();
            subjectNode.parse(context);
            predicateNode.parse(context);
      }
      public void execute(){
           subjectNode.execute();
           predicateNode.execute();
     }
} 

Nonterminal Expression:非终结符表达式_2

public class SubjectNode implements Node{
      Node node;
      public void parse(Context context){
            node =new SubjectPronounOrNounNode();
            node.parse(context);
     }
      public void execute(){
           node.execute();
     }
} 

Nonterminal Expression:非终结符表达式_3

public class PredicateNode implements Node{
      Node verbNode,objectNode;
      public void parse(Context context){
            verbNode =new VerbNode();
            objectNode=new ObjectNode();
            verbNode.parse(context);
            objectNode.parse(context);
      }
      public void execute(){
          verbNode.execute();
          objectNode.execute();
     }
} 

Nonterminal Expression:非终结符表达式_4

public class ObjectNode implements Node{
      Node node;
      public void parse(Context context){
            node =new ObjectPronounOrNounNode();
            node.parse(context);
     }
      public void execute(){
           node.execute();
     }
}

上下文Context

import java.util.StringTokenizer;
public class Context{
      StringTokenizer tokenizer;
      String token;
      public Context(String text){
           setContext(text);
     } 
     public void setContext(String text){
          tokenizer=new StringTokenizer(text);
     }
      String nextToken(){
            if(tokenizer.hasMoreTokens()){
                 token=tokenizer.nextToken(); 
           }
           else
                token="";
           return token;
      } 
}

运行类

public class Application{
     public static void main(String args[]){
          String text="Teacher beat tiger";         
          Context context=new Context(text);
          Node node=new SentenceNode();
          node.parse(context);
          node.execute();

          text="You eat  apple";
          context.setContext(text);
          System.out.println();
          node.parse(context);
          node.execute();

           text="you look  him";
          context.setContext(text);
          System.out.println();
          node.parse(context);
          node.execute();
     }
}

4.总结

优点

image.png

缺点

上一篇 下一篇

猜你喜欢

热点阅读