/** * Declares an abstract interpretation operation * @author stone * */public interface Interpreter {public void interpret (context context); In practice, you can have a returned type that defines the data object that is interpreted}
public class Xmlsaxinterpreter implements interpreter {@Overridepublic void interpret (context context) { SYSTEM.OUT.PRINTLN ("XML SAX Interpreter:" + context.getdata ());}}
public class Xmldominterpreter implements interpreter {@Overridepublic void interpret (context context) { SYSTEM.OUT.PRINTLN ("XML DOM Interpreter:" + context.getdata ());}}
/** * Contains some information outside of the interpreter * @author Stone * */public class Context {private string Data;public string GetData () {return data;} public void SetData (String data) {this.data = data;}}
/* Interpreter (Interpreter) mode * Given a language, define a representation of its grammar and define an interpreter that uses that representation to interpret sentences in the language. is a behavioral pattern * application, such as compiler, regular expression, language specification ... * interpreter mode is used very little in actual system development because it can cause problems such as efficiency, performance, and maintenance, */public class Test {public static void main (string[] args) {Context context = new context (); Context.setdata ("XML data"); new Xmlsaxinterpreter (). interpret (context); new Xmldominterpreter (). interpret (context);}}
Java implementation Interpreter (interpreter) mode