Given a language, the Interpreter design pattern defines a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
简单来说~ 解释器能定义几个解释的方法~ 然后再将要解释的方法丢进去解释~
学习目标: 解释器模式的概念及实务
学习难度: ☆☆☆
using System;using System.Collections.Generic;namespace ConsoleApp1{ public class Player { public string name="贾斯汀"; } //声明一个用于执行操作的接口 public abstract class AbstractExpression { public abstract void Interpret(Player player); } //实体翻译方法 public class EnglishExpression : AbstractExpression { public override void Interpret(Player player) { player.name = "Justin"; Console.WriteLine(player.name); } } public class MainProgram { public static void Main(string[] args) { Player player = new Player(); Console.WriteLine(player.name); AbstractExpression englishExpression = new EnglishExpression(); englishExpression.Interpret(player); } }}
参考资料:
https://www.dofactory.com/net/interpreter-design-pattern