飞道的博客

在王者荣耀角度下分析面向对象程序设计B中23种设计模式之解释器模式

523人阅读  评论(0)

·

解释器模式在王者荣耀中的应用

·

一、简述

在王者荣耀手游中,有局内语音转文字功能,玩家通过使用普通话在5秒的倒计时内进行简短而有效的语音输入,系统会自动将玩家说的话在极短时间内转换成文字内容,然后点击发送,玩家的文字聊天信息就发送出去,其他玩家就可以以文字读取方式了解你要传达的信息了。

在本实例中,我们通过实现局内语音转文字功能进而说明解释器模式的应用实例。具体而言,当我们在语音转文字过程中是需要解释执行的,这样我们可以将转文字中每一个字句表示为一个类。但是由于汉语结构、规则复杂,过多的字句组合使得我们很难维护所有的类,所以在这里我们只简单做5个字的语音转换来说明解释器模式的应用实例。
对于这一问题,如果使用程序实现这一效果的话,定义的基本操作是将语音的值转换为汉语文字,比如,发音“zhong”就解释执行为“中”,发音“le”就解释执行为“了”……这样一来,当玩家通过语音说“zhong lu yi bo le”时,程序就解释执行输出“中路一波了”。

二、解释器模式(Interpreter Pattern)

解释器模式理解:
高度概括:给定一个语言,定义他的文法的一种表示并定义一个解释器,这个解释器使用该表是来解释语言中的句子。
对于某些问题,我们可能希望用简单的语言来描述及希望用简单的语言来实现一些操作,比如使用简单语言,实现简单的翻译操作,解释模式是关于怎样实现一个简单语言的陈述模式,其关键是将每一个语法规则表示成一个类。

解释器模式结构中的四种角色:
①抽象表达式(AbstractExpression):该角色为一个接口,负责定义抽象的解释操作;
②终结符表达式(TerminalExpression):实现抽象表达式接口的类,该类将接口中的解释操作实现为与文法中的终结符相关联的操作,即文法中每个终结符号需要一个终结符表达式类;
③非终结符表达式(NonterminalExpression):实现抽象表达式接口的类,文法中的每一条规则都需要非终结符表达式类。非终结符表达式类为文法中的非终结符号实现解释操作,该解释操作通常使用递归调用表示对象的解释操作;
④上下文(Context):包含解释器之外的一些全局信息。

解释器模式的UML类图:

解释器模式的优缺点:
优点:
将每一个语法规则表示成一个类,方便实现简单的语言;
由于使用肋表示语法规则可以较容易改变或扩展语言的行为;
通过在类结构中加入新的方法,可以在解释的同时增加新的行为;
缺点:
如果文法过于复杂,那么过多的文法规则是我们很难维护所给出的类;
执行效率较低。

解释器模式的适用情景:
当有一个简单的语言需要解释执行,并且可以将该语言的每一个规则表示为一个类时,就可以使用解释器模式。
 

三、王者荣耀角度下实现解释器模式结构图及代码

eclipse结构图

主函数【应用(Application)】
Applicayion.java


  
  1. public class Application{
  2. public static void main(String args[]){
  3. System.out.println( "①当玩家使用“普通话”向队友发送消息时:");
  4. System.out.println( " 【系统提示】正在打开麦克风……");
  5. System.out.println( " 【系统提示】语音录入中……");
  6. System.out.println( " 【系统提示】正在转换……");
  7. System.out.print( " 【系统提示】转换成功:");
  8. String text= "Zhong Lu Yi Bo Le";
  9. Context context= new Context(text);
  10. Node node= new OneTwoNode();
  11. node.parse(context);
  12. node.execute();
  13. System.out.println( "");
  14. System.out.println( "");
  15. System.out.println( "★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
  16. System.out.println( "");
  17. System.out.println( "②当玩家使用“方言”向队友发送消息时:");
  18. System.out.println( " 【系统提示】正在打开麦克风……");
  19. System.out.println( " 【系统提示】语音录入中……");
  20. System.out.println( " 【系统提示】正在转换……");
  21. System.out.print( " 【系统提示】转换成功:");
  22. text= "Ji He Jin Gong Zhu Zai";
  23. context= new Context(text);
  24. node= new OneTwoNode();
  25. node.parse(context);
  26. node.execute();
  27. }
  28. }

抽象表达式(AbstractExpression)
Node.java


  
  1. public interface Node{
  2. public void parse(Context text);
  3. public void execute();
  4. }

终结符表达式(TerminalExpression)
OneNode.java


  
  1. public class OneNode implements Node{
  2. String [] word={ "Zhong", "", "", "", ""};
  3. String token;
  4. boolean boo;
  5. public void parse(Context context){
  6. token=context.nextToken();
  7. int i= 0;
  8. for(i= 0;i<word.length;i++){
  9. if(token.equalsIgnoreCase(word[i])){
  10. boo= true;
  11. break;
  12. }
  13. }
  14. if(i==word.length)
  15. boo= false;
  16. }
  17. public void execute(){
  18. if(boo){
  19. if(token.equalsIgnoreCase(word[ 0]))
  20. System.out.print( "中");
  21. }
  22. }
  23. }

TwoNode.java 


  
  1. public class TwoNode implements Node{
  2. String [] word={ "Lu", "", "", "", ""};
  3. String token;
  4. boolean boo;
  5. public void parse(Context context){
  6. token=context.nextToken();
  7. int i= 0;
  8. for(i= 0;i<word.length;i++){
  9. if(token.equalsIgnoreCase(word[i])){
  10. boo= true;
  11. break;
  12. }
  13. }
  14. if(i==word.length)
  15. boo= false;
  16. }
  17. public void execute(){
  18. if(boo){
  19. if(token.equalsIgnoreCase(word[ 0]))
  20. System.out.print( "路");
  21. }
  22. }
  23. }

ThreeNode.java


  
  1. public class TwoNode implements Node{
  2. String [] word={ "Lu", "", "", "", ""};
  3. String token;
  4. boolean boo;
  5. public void parse(Context context){
  6. token=context.nextToken();
  7. int i= 0;
  8. for(i= 0;i<word.length;i++){
  9. if(token.equalsIgnoreCase(word[i])){
  10. boo= true;
  11. break;
  12. }
  13. }
  14. if(i==word.length)
  15. boo= false;
  16. }
  17. public void execute(){
  18. if(boo){
  19. if(token.equalsIgnoreCase(word[ 0]))
  20. System.out.print( "路");
  21. }
  22. }
  23. }

ForeNode.java


  
  1. public class ForeNode implements Node{
  2. String [] word={ "", "", "", "Bo", ""};
  3. String token;
  4. boolean boo;
  5. public void parse(Context context){
  6. token=context.nextToken();
  7. int i= 0;
  8. for(i= 0;i<word.length;i++){
  9. if(token.equalsIgnoreCase(word[i])){
  10. boo= true;
  11. break;
  12. }
  13. }
  14. if(i==word.length)
  15. boo= false;
  16. }
  17. public void execute(){
  18. if(boo){
  19. if(token.equalsIgnoreCase(word[ 3]))
  20. System.out.print( "波");
  21. }
  22. }
  23. }

FiveNode.java


  
  1. public class FiveNode implements Node{
  2. String [] word={ "", "", "", "", "Le"};
  3. String token;
  4. boolean boo;
  5. public void parse(Context context){
  6. token=context.nextToken();
  7. int i= 0;
  8. for(i= 0;i<word.length;i++){
  9. if(token.equalsIgnoreCase(word[i])){
  10. boo= true;
  11. break;
  12. }
  13. }
  14. if(i==word.length)
  15. boo= false;
  16. }
  17. public void execute(){
  18. if(boo){
  19. if(token.equalsIgnoreCase(word[ 4]))
  20. System.out.print( "了");
  21. }
  22. else{
  23. System.out.print( "呜呜呜~~~什么都没听清┭┮﹏┭┮");
  24. }
  25. }
  26. }

非终结符表达式(NonterminalExpression)
FirstNode.java


  
  1. public class FirstNode implements Node{
  2. Node node;
  3. public void parse(Context context){
  4. node = new OneNode();
  5. node.parse(context);
  6. }
  7. public void execute(){
  8. node.execute();
  9. }
  10. }

TwiceNode.java


  
  1. public class TwiceNode implements Node{
  2. Node node;
  3. public void parse(Context context){
  4. node = new TwoNode();
  5. node.parse(context);
  6. }
  7. public void execute(){
  8. node.execute();
  9. }
  10. }

ThirdNode.java


  
  1. public class ThirdNode implements Node{
  2. Node node;
  3. public void parse(Context context){
  4. node = new ThreeNode();
  5. node.parse(context);
  6. }
  7. public void execute(){
  8. node.execute();
  9. }
  10. }

ForthNode.java


  
  1. public class ForthNode implements Node{
  2. Node node;
  3. public void parse(Context context){
  4. node = new ForeNode();
  5. node.parse(context);
  6. }
  7. public void execute(){
  8. node.execute();
  9. }
  10. }

FifthNode.java


  
  1. public class FifthNode implements Node{
  2. Node node;
  3. public void parse(Context context){
  4. node = new FiveNode();
  5. node.parse(context);
  6. }
  7. public void execute(){
  8. node.execute();
  9. }
  10. }

OneTwoNode.java 


  
  1. public class OneTwoNode implements Node{
  2. Node one,twoThree;
  3. public void parse(Context context){
  4. one = new FirstNode();
  5. twoThree= new TwoThreeNode();
  6. one.parse(context);
  7. twoThree.parse(context);
  8. }
  9. public void execute(){
  10. one.execute();
  11. twoThree.execute();
  12. }
  13. }

TwoThreeNode.java 


  
  1. public class TwoThreeNode implements Node{
  2. Node two,threeFore;
  3. public void parse(Context context){
  4. two = new TwiceNode();
  5. threeFore= new ThreeForeNode();
  6. two.parse(context);
  7. threeFore.parse(context);
  8. }
  9. public void execute(){
  10. two.execute();
  11. threeFore.execute();
  12. }
  13. }

ThreeForeNode.java 


  
  1. public class ThreeForeNode implements Node{
  2. Node three,foreFive;
  3. public void parse(Context context){
  4. three = new ThirdNode();
  5. foreFive= new ForeFiveNode();
  6. three.parse(context);
  7. foreFive.parse(context);
  8. }
  9. public void execute(){
  10. three.execute();
  11. foreFive.execute();
  12. }
  13. }

ForeFiveNode.java 


  
  1. public class ForeFiveNode implements Node{
  2. Node fore,five;
  3. public void parse(Context context){
  4. fore = new ForthNode();
  5. five= new FifthNode();
  6. fore.parse(context);
  7. five.parse(context);
  8. }
  9. public void execute(){
  10. fore.execute();
  11. five.execute();
  12. }
  13. }

上下文(Context)
Context.java


  
  1. import java.util.StringTokenizer;
  2. public class Context{
  3. StringTokenizer tokenizer;
  4. String token;
  5. public Context(String text){
  6. setContext(text);
  7. }
  8. public void setContext(String text){
  9. tokenizer= new StringTokenizer(text);
  10. }
  11. String nextToken(){
  12. if(tokenizer.hasMoreTokens()){
  13. token=tokenizer.nextToken();
  14. }
  15. else
  16. token= "";
  17. return token;
  18. }
  19. }

运行结果截图

更多设计模式在王者荣耀中的应用请点击我的→设计模式在王者荣耀中的应用专栏。

欢迎留言,一起学习交流~

感谢阅读

END

 


转载:https://blog.csdn.net/IT_charge/article/details/105795494
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场