Java 23 design patterns-23. The interpreter pattern of the behavioral pattern

In addition to the design principles in Java, there are also 23 design patterns.

These patterns have been accumulated by the predecessors bit by bit, have been improving, and have been optimizing, and these design patterns can solve some specific problems.

And in these modes, it can be said that the use of language is fully reflected.

Then we are going to learn   the interpreter mode in the   behavioral mode today     !

Interpreter mode

Let's first take a look at what is the interpreter mode

Through the encyclopedia, we can see that there is nothing, here I will tell you what is called the interpreter mode, and what this mode is mainly used for

This mode mainly uses some identical or similar words and characters, we will explain these characters and words, and then operate

Definition and characteristics of interpreter mode

Definition of Interpreter mode: Define a language for the analysis object, define the grammatical representation of the language, and then design a parser to interpret the sentences in the language. In other words, use the compiled language to analyze the examples in the application. This mode implements an interface for grammatical expression processing, which interprets a specific context.

The concept of grammar and sentence mentioned here is the same as the description in the principle of compilation. "Grammar" refers to the grammatical rules of the language, and "sentence" is an element in the language set. For example, there are many sentences in Chinese, and "I am a Chinese" is one of the sentences. A grammar tree can be used to intuitively describe the sentences in the language.

The interpreter mode is a behavior-like mode, and its main advantages are as follows.

1. Good scalability. Since classes are used to represent the grammar rules of the language in the interpreter mode, the grammar can be changed or extended through mechanisms such as inheritance.

2. Easy to implement. Each expression node class in the syntax tree is similar, so it is easier to implement its grammar.


The main disadvantages of the interpreter mode are as follows.

1. The execution efficiency is low. The interpreter mode usually uses a large number of loops and recursive calls. When the sentence to be explained is more complicated, its running speed is very slow, and the debugging process of the code is also more troublesome.

2. Will cause class expansion. Each rule in the interpreter mode needs to define at least one class. When there are many grammatical rules, the number of classes will increase sharply, making the system difficult to manage and maintain.

3. There are fewer applicable scenarios. In software development, there are very few application examples that need to define language grammar, so this mode is rarely used.

The structure and realization of the interpreter pattern

There is one more thing to say about this model, which is grammar

grammar

Grammar is the formal rules used to describe the grammatical structure of a language. There is no rule without rules. For example, some people think that the rule of perfect love is "attractive each other, single-minded affection, and no love experience in either party." Although the last rule is harsher, there must be rules for everything, and the language is the same. Regardless of whether it is machine language or natural language, it has its own grammatical rules.

〈句子〉::=〈主语〉〈谓语〉〈宾语〉
〈主语〉::=〈代词〉|〈名词〉
〈谓语〉::=〈动词〉
〈宾语〉::=〈代词〉|〈名词〉
〈代词〉你|我|他
〈名词〉7大学生I筱霞I英语
〈动词〉::=是|学习

Let’s take a look at the grammar a little bit briefly, and then let’s take a look at the characters

1. Abstract expression role: defines the interface of the interpreter, agrees on the interpretation operation of the interpreter, and mainly includes the interpretation method interpret().

2. Terminal expression role: It is a subclass of abstract expressions, used to implement operations related to terminal symbols in the grammar. Each terminal symbol in the grammar has a specific terminal expression corresponding to it.

3. Non-terminal symbol expression role: It is also a subclass of abstract expressions, used to implement operations related to non-terminal symbols in the grammar. Each rule in the grammar corresponds to a non-terminal symbol expression.

4. Environmental role: It usually contains data or common functions required by each interpreter. It is generally used to transfer data shared by all interpreters. Later interpreters can obtain these values ​​from here.

5. Client: The main task is to convert the sentence or expression that needs to be analyzed into an abstract syntax tree described by the interpreter object, and then call the interpretation method of the interpreter. Of course, the interpretation method of the interpreter can also be accessed indirectly through the environment role.

 

Let's make a simple example, just calculate the bank deposit interest

first step:

Let's first define an abstract expression role

This is mainly used to judge whether the string we passed in meets the conditions of the corresponding expression

The second step:

Create terminator expression roles and implement abstract expression roles

Here we need to store the characters we want to judge, and store them in the Set collection, so that the uniqueness of the data can be guaranteed, and the judgment will not be repeated. The implementation method mainly judges whether it exists

third step:

Create non-terminal expression roles, and also implement abstract expression roles

Here we pass in an entity class of terminal expression at the time, and then judge whether they all exist in the Set collection just now. If they all exist, it means that our text can be explained.

the fourth step:

Create the environment

Here we need to define the formula. Of course, we must have more than one formula. If there are more than one, we will write multiple arrays.

 expression is a non-terminal character expression role. Through this we can detect whether the formula currently passed in matches or not. If it matches, we obtain the principal, interest rate, and time for multiplication, and output

test:

Let's get a Test class to test

Output it

As you can see, there is no problem with the calculation result

 

OK, that's it, everyone take a good look. Practice a lot. If you have any questions, please contact me QQ: 2100363119

This is the last pattern in the design patterns. I hope you will look at the design patterns more. To be honest, if you are proficient in Java design patterns during the interview, then I believe your code is not bad.

Finally, welcome everyone to visit my personal website: lemon1234.com Thank you for leaving a message

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/109696919