解释器模式:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
/**
* @author stefanie zhao
* @date 2014-8-21 下午04:37:29
*/
public class Context {
private String input;
private String output;
/**
* @return the input
*/
public String getInput() {
return input;
}
/**
* @param input
* the input to set
*/
public void setInput(String input) {
this.input = input;
}
/**
* @return the output
*/
public String getOutput() {
return output;
}
/**
* @param output
* the output to set
*/
public void setOutput(String output) {
this.output = output;
}
}
/**
* @author stefanie zhao
* @date 2014-8-21 下午04:37:03
*/
public abstract class AbstractExpression {
public abstract void interpret(Context context);
}
/**
* @author stefanie zhao
* @date 2014-8-21 下午04:38:16
*/
public class NonterminalExpression extends AbstractExpression {
public void interpret(Context context) {
// TODO Auto-generated method stub
System.out.println("非终端解释器");
}
}
/**
* @author stefanie zhao
* @date 2014-8-21 下午04:38:16
*/
public class TerminalExpression extends AbstractExpression {
public void interpret(Context context) {
// TODO Auto-generated method stub
System.out.println("终端解释器");
}
}
public class Main {
/**
* @Description: TODO
* @param @param args
* @return void
* @throws
*/
public static void main(String[] args) {
Context context = new Context();
List<AbstractExpression> list = new ArrayList<AbstractExpression>();
list.add(new TerminalExpression());
list.add(new NonterminalExpression());
list.add(new TerminalExpression());
list.add(new TerminalExpression());
for (AbstractExpression a : list)
a.interpret(context);
}
}