Interpreter pattern (interpreter pattern): Given a language, defines a representation of its grammar and defines an interpreter that uses that representation to interpret sentences in the language.
Here is a demo of the Interpreter mode:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 __author__='Andy'5 """6 Liar design Mode7 design mode-interpreter mode8 interpreter pattern (interpreter pattern): Given a language, defines a representation of its grammar and defines an interpreter that uses that representation to interpret sentences in the language.9 """Ten #abstract an interpreter class One classabstractexpression (object): A - defInterpreter (self, context): - Pass the - #specific interpreter--terminal inheritance abstract interpreter - classterminalexpression (abstractexpression): - + defInterpreter (self, context): - Print "Terminal Interpreter", Context + A #specific interpreter--terminal inheritance abstract interpreter at classnotterminalexpression (abstractexpression): - - defInterpreter (self, context): - Print "non-terminal interpreter", Context - - in classContext (object): - to def __init__(self): +Self.name ="" - the if __name__=="__main__": *Context =Context () $Context.name ='Andy'Panax NotoginsengArr_list =[Notterminalexpression (), Terminalexpression (), Terminalexpression ()] - forEntryincharr_list: theEntry.interpreter (context)
The design of the above class is as follows:
When a language needs to be executed and a sentence of that language can be expressed as an abstract syntax tree, you can use the interpreter pattern
Andy
Source: http://www.cnblogs.com/onepiece-andy/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the author's consent must be in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Liar design Pattern Python implementation-interpreter mode