Design Mode 11

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.