Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the interpreter pattern of Java design pattern

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to understand the interpreter pattern of the Java design pattern". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the interpreter pattern of the Java design pattern.

I. what is the interpreter mode

Definition: given a language, define a representation of a grammar and define an interpreter that uses the representation to interpret sentences in the language.

The roles involved in the interpreter pattern are as follows:

(1) Abstract expression (Expression) role: declare an abstract interface that all concrete expression roles need to implement. This interface is mainly an interpret () method, called an interpretive operation.

(2) Terminator expression (Terminal Expression) role: implements the interface required by the abstract expression role, mainly an interpret () method; each Terminator in the grammar has a concrete terminal expression corresponding to it. For example, there is a simple formula R=R1+R2, where R1 and R2 are the Terminators, and the corresponding interpreter that parses R1 and R2 is the Terminator expression.

(3) non-Terminator expression (Nonterminal Expression) role: every rule in the grammar requires a specific non-Terminator expression, which is generally an operator or other keyword in the grammar, such as the formula R=R1+R2, "+" is a non-Terminator, and the interpreter that parses "+" is a non-Terminator expression.

(4) Context role: the task of this role is to store the specific values corresponding to each Terminator in the grammar, such as R=R1+R2. We assign 100,200 to R1 and 200 to R2. This information needs to be stored in the environment role, and in many cases it is sufficient for us to use Map to play the environment role.

Second, the use scenario of the interpreter mode

1. When there is a language that needs to be interpreted and executed, and you can represent sentences in that language as an abstract syntax tree, you can use the interpreter pattern. This mode works best when the following conditions exist

two。 The class hierarchy of the grammar becomes too large to manage. At this point, tools such as the parser generator are the best choice. They can interpret expressions without building an abstract syntax tree, which saves space and possibly time.

3. Efficiency is not a key issue, and the most efficient interpreters are not usually implemented by directly interpreting the parsing tree, but by first loading them into another form. For example, regular expressions are usually replaced by state machines. Even in this case, the converter can still be implemented in the interpreter pattern, which is still useful.

Third, the advantages and disadvantages of the interpreter mode:

1. Methods can be easily changed and extended because the pattern uses classes to represent method rules, and you can use inheritance to change or extend the method.

two。 It is also easier to implement methods, because the implementation of the classes that define each node of the abstract syntax tree is generally similar, and these classes are easy to write directly.

3. The interpreter mode is to transform a sentence into an actual command program execution. It can be analyzed without the interpreter pattern itself, but it is convenient to expand and maintain the grammar by inheriting the abstract expression and relying on the transpose principle.

Disadvantages:

The interpreter pattern defines at least one class for each rule in a method, so methods that contain many rules can be difficult to manage and maintain. So when the method is very complex, use other techniques such as a parser or compiler generator to deal with it.

Fourth, the implementation of interpreter mode music interpreter performance content class (Context)

/ / performance content class (Context) class PlayContext {/ / performance text private string text; public string PlayText {get {return text;} set {text = value;}} expression class (AbstractExpression) / expression class (AbstractExpression) abstract class Expression {/ / interpreter public void Interpret (PlayContext context) {if (context.PlayText.Length = = 0) return String playKey = context.PlayText.Substring (0,1); context.PlayText = context.PlayText.Substring (2); double playValue = Convert.ToDouble (context.PlayText.Substring (0, context.PlayText.IndexOf ("); context.PlayText = context.PlayText.Substring (context.PlayText.IndexOf (") + 1); Excute (playKey, playValue);} / execute public abstract void Excute (string key, double value) } TerminaExperssion / / TerminaExperssion class Note: Expression {public override void Excute (string key, double value) {string note = ""; switch (key) {case "C": note = "1"; break; case "D": note = "2"; break Case "E": note = "3"; break; case "F": note = "4"; break; case "G": note = "5"; break; case "A": note = "6" Break; case "B": note = "7"; break;} / / TerminaExperssion class Scale: Expression {public override void Excute (string key, double value) {string scale = "" Switch ((int) value) {case 1: scale = "bass"; break; case 2: scale = "alto"; break; case 3: scale = "treble"; break } client code class Program {/ / client code static void Main (string [] args) {PlayContext context = new PlayContext (); context.PlayText = "O 2 E 0.5G 0.5 A 3 E 0.5"; Expression expression = null Try {while (context.PlayText.Length > 0) {string str = context.PlayText.Substring (0,1); switch (str) {case "O": expression = new Scale (); break Case "P": / / when the initials are CDEFGAB and rest P, instantiate the note expression = new Note (); break;} expression.Interpret (context);}} catch (Exception) {throw } Console.Read ();}} at this point, I believe you have a deeper understanding of "how to understand the interpreter pattern of the Java design pattern". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report