I'm not sure this pattern has wide applicability other than to write language processors for simple languages.
The three main players are Context, Expression, and Value. In order to evaluate an expression, such as "x + 2" we need to know the value of x. The context might contain symbol tables that tells us that x = 5 and that "+" represents the addition operation. Therefore, the value produced by evaluating "x + 2" is 7.
In summary, expressions produce values when they are evaluated. Evaluating an expression may require some sort of context to determine values of symbols.
The interpreter (which could be any type of language processor) creates and evaluates expressions relative to its context.
There are two types of expressions: terminal expressions that don't contain sub-expressions (such as symbols and literals) and non-terminal expressions that do contain sub-expressions (such as function calls and control structures). This means that the interpreter pattern is really a special case of the Composite pattern:
There is a similarity between the Interpreter pattern and the Command Processor pattern.
Here's a sketch of a simple interpreter:
public class Interpreter {
private Context context;
private Scanner scanner;
private Parser parser;
public void controlLoop() {
while(true) {
System.out.print("->
"); // display prompt
String tokens = scanner.read();
// read user input
if
(tokens.equals("quit")) break;
AbstractExpression exp = parser.parse(tokens);
Value val = exp.eval(context);
System.out.println(value);
}
}
}