解释器模式

在计算机编程中，解释器模式是一种设计模式，它规定了如何求值一个语言中的句子。基本想法是使专门计算机语言的每个符号（终结符与非终结符）都有一个类。这个语言中句子的语法树是合成模式的一个实例，它被用来为客户求值（解释）这个句子。
结构.
在上面的UML类图中：
对象图展示了运行时交互：
示例.
C++.
下面的例子是基于《设计模式》书中先于C++98的样例代码的C++11实现。
class Context;
class BooleanExp {
public:
 BooleanExp() = default;
 virtual ~BooleanExp() = default;
 virtual bool evaluate(Context&amp;) = 0;
 virtual BooleanExp* replace(const char*, BooleanExp&amp;) = 0;
 virtual BooleanExp* copy() const = 0;
class VariableExp;
class Context {
public:
private:
 std::map&lt;const VariableExp*, bool&gt; m;
class VariableExp : public BooleanExp {
public:
 VariableExp(const char* name_) :name(nullptr) {
 name = strdup(name_);
 virtual ~VariableExp() = default;
 virtual bool evaluate(Context&amp; aContext) {
 return aContext.lookup(this);
 virtual BooleanExp* replace( const char* name_, BooleanExp&amp; exp ) {
 if (0 == strcmp(name_, name)) {
 return exp.copy();
 } else {
 return new VariableExp(name);
 virtual BooleanExp* copy() const {
 return new VariableExp(name);
 VariableExp(const VariableExp&amp;) = delete; // rule of three
 VariableExp&amp; operator=(const VariableExp&amp;) = delete;
private:
 char* name;
class AndExp : public BooleanExp {
public:
 AndExp(BooleanExp* op1, BooleanExp* op2)
 :operand1(nullptr), operand2(nullptr) {
 operand1 = op1;
 operand2 = op2;
 virtual ~AndExp() = default;
 virtual bool evaluate(Context&amp; aContext) {
 return operand1-&gt;evaluate(aContext) &amp;&amp; operand2-&gt;evaluate(aContext);
 virtual BooleanExp* replace(const char* name_, BooleanExp&amp; exp) {
 return new AndExp(
 operand1-&gt;replace(name_, exp),
 operand2-&gt;replace(name_, exp)
 virtual BooleanExp* copy() const {
 return new AndExp(operand1-&gt;copy(), operand2-&gt;copy());
 AndExp(const AndExp&amp;) = delete; // rule of three
 AndExp&amp; operator=(const AndExp&amp;) = delete;
private:
 BooleanExp* operand1;
 BooleanExp* operand2;
int main() {
 BooleanExp* expression;
 Context context;
 VariableExp* x = new VariableExp("X");
 VariableExp* y = new VariableExp("Y");
 expression = new AndExp(x, y);
 context.assign(x, false);
 context.assign(y, true);
 bool result = expression-&gt;evaluate(context);
 std::cout « result « '\n';
 context.assign(x, true);
 context.assign(y, true);
 result = expression-&gt;evaluate(context);
 std::cout « result « '\n';
程序输出为：
0
1
Python.
下面是Python的例子：
from abc import ABC, abstractmethod
class Context():
 def __init__(self):
 def lookup(self, key):
 return (self.var_dict[key.name]
 if key.name in self.var_dict else None)
 def assign(self, key, value):
 self.var_dict[key.name] = value
class BooleanExp(ABC):
 @abstractmethod
 def evaluate(self): pass
 @abstractmethod
 def replace(self): pass
 @abstractmethod
 def copy(self): pass
class VariableExp(BooleanExp):
 def __init__(self, name):
 self.name = name
 def __str__(self):
 return f"({self.name})"
 def evaluate(self, context):
 return context.lookup(self)
 def replace(self, name, exp):
 return (exp.copy() if self.name == name
 else type(self)(self.name))
 def copy(self):
 return type(self)(self.name)
class AndExp(BooleanExp):
 def __init__(self, opd1=None, opd2=None):
 assert opd2 is not None
 self.opd1 = opd1
 self.opd2 = opd2 
 def __str__(self):
 return f"({self.opd1} and {self.opd2})"
 def evaluate(self, context):
 opd1 = self.opd1.evaluate(context)
 opd2 = self.opd2.evaluate(context)
 return opd1 and opd2 
 def replace(self, name, exp):
 opd1 = self.opd1.replace(name, exp)
 opd2 = self.opd2.replace(name, exp)
 return type(self)(opd1, opd2)
 def copy(self):
 opd1 = self.opd1.copy()
 opd2 = self.opd2.copy()
 return type(self)(opd1, opd2)
def demo():
 context = Context()
 x = VariableExp("X")
 y = VariableExp("Y")
 exp = AndExp(x, y)
 print(exp)
 context.assign(x, False)
 context.assign(y, True)
 print(exp.evaluate(context))
 context.assign(x, True)
 context.assign(y, True)
 print(exp.evaluate(context))
 z = VariableExp("Z")
 exp1 = exp.replace("X", z)
 print(exp1)
 print(exp1.evaluate(context))
 context.assign(z, False)
 print(exp1.evaluate(context))
其输出：
»&gt; demo()
False
True
None
False