22. interpreter Mode
The interpreter mode (Interpreter) defines a syntax expression of a language and an interpreter, which uses this expression to explain sentences in a language.
A) Advantages:
I. When the interpreter mode is used, it means it is easy to change and extend grammar. Because this mode uses classes to represent grammar rules, you can use inheritance to change or extend grammar. It is also easier to implement grammar, because the implementation of classes on each node in the definition abstract syntax tree is similar in general, and these classes are easy to directly write.
B) Disadvantages:
I. The Interpreter mode defines at least one class for each rule in the grammar, so the grammar containing many rules may be difficult to manage and maintain. It is recommended that you use other technologies such as syntax analysis programs or compiler generators to process the syntax when it is very complex.
C) Application of interpreter mode:
I. When a language needs to be interpreted and executed, and you can represent sentences in the language as an abstract syntax tree, you can use the interpreter mode.
23. Visitor Mode
The visitor mode (vistor) indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.
A) Advantages:
I. The visitor mode frees the coupling between the data structure and the operations acting on the structure, allowing the operation set to evolve relatively freely.
II. It is easy to add new operations in the visitor mode, because adding new operations means adding a new visitor. The visitor mode aggregates relevant behaviors into a visitor object.
B) Disadvantages:
I. It is difficult to add new data structures.
C) Application of visitor mode:
I. The visitor mode is suitable for systems with relatively stable data structures.
II. If the system has a stable data structure and an algorithm that is easy to change, it is more suitable to use the visitor mode, because the visitor mode makes it easier to increase algorithm operations.