解释器模式:
给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子
public interface Expression {
public void interpreter(Context context);
}
public class Context {
private String out;
public String getOut() {
return out;
}
public void setOut(String out) {
this.out = out;
}
}
public class NormalExpression implements Expression{
@Override
public void interpreter(Context context) {
System.out.println("Normal Expression.");
}
}
public class SpecialExpression implements Expression{
@Override
public void interpreter(Context context) {
System.out.println("Special Expression.");
}
}
public class InterpreterDemo {
public static void main(String[] args) {
List<Expression> list = new ArrayList<>();
Context context = new Context();
list.add(new NormalExpression());
list.add(new SpecialExpression());
list.add(new NormalExpression());
list.add(new SpecialExpression());
for (Expression expression : list) {
expression.interpreter(context);
}
}
}