Tutorials to .com

Tutorials to .com » Jsp » Skills » To explain in layman's language and Java-based design model

To explain in layman's language and Java-based design model

Print View , by: iSee ,Total views: 2 ,Word Count: 1517 ,Date: Thu, 9 Jul 2009 Time: 9:23 PM

I. Introduction

In fact, there is no good example to explain the introduction of mode, because it describes how to form a simple language parser, mainly used in the development of the use of object-oriented language of the compiler; in practical applications, we may have very little structure to meet grammar of a language situation.

Although you use less than this model is almost, but still able to take a look inspired by some.

Second, the definition and structure

To explain the definition of mode as follows: the definition of language, grammar, and the establishment of an interpreter to interpret the language of the sentence. It is a kind of behavioral patterns. The meaning of the language here is to use the provisions of the code format and syntax.

Pointed out in the GOF book: If a specific type of problem frequency high enough, then the issue may be worthy of all expressed as a simple example of the language of the sentence. This will build a parser, the interpreter to interpret the sentence passed to solve the problem. And when the grammar is simple, efficient key issue is not the best.

This is the explanation of the application mode environment.

Let us take a look at the interpretation of the mysterious mode is composed of what you.

1) the role of abstract expression: statement of an abstract interpretation of the operation, the interface for all the specific expression of the role of (abstract syntax tree nodes) to be achieved.

What is an abstract syntax tree? "Java and model" of explanation to is: the abstract syntax tree of each node represents a sentence, and in each node can be the implementation of the interpretation. The interpretation of this statement on behalf of the Executive be interpreted. As a statement on behalf of this statement are to be explained. Because each statement represents a common example of the problem, each node on the interpretation of an operation represents an example of the answer to a question.

2) expression at the end of the role: specific expression.

a) Implementation and grammar at the end of the operation associated with the interpretation

b) and each sentence at the end of such an example of the need for the corresponding

3) the role of non-terminator expression: specific expression.

a) the rules of grammar for each of the R:: = R1R2 ... Rn need a strap-style non-role at the end of

b) from R1 to Rn are each symbolic role in the maintenance of an abstract expression of the instance variables

c) explain the operation to achieve, to explain to the general recursive call to that from the R1 to Rn to explain the object of those operations

4) the context (environment) Role: Interpreter contains some global information outside.

5) Guest role:

a) to build (or was given) that the definition of the grammar of the language of a particular sentence of the abstract syntax tree

b) explain the operation of call

Zhang explained that the structure placed on the class diagram it is also from the GOF book.



For each role are given in detail the responsibilities, but also in the class diagram given in the relationship between the five roles. To achieve this is not very difficult, the following gives a simple example, you would like to deepen the understanding of the interpretation mode.

Third, for example

To give you an example of calculation, the realization of ideas from the "java and model" in the example. Functional role of each in accordance with the above-mentioned norms to achieve.

/ / The context (environment) role, use HashMap to store the corresponding numerical variables

class Context
(
private Map valueMap = new HashMap ();
public void addValue (Variable x, int y)
(
Integer yi = new Integer (y);
valueMap.put (x, yi);
)

public int LookupValue (Variable x)
(
int i = ((Integer) valueMap.get (x)). intValue ();
return i;
)
)

/ / Abstract expression of the role of interface can also be used to achieve

abstract class Expression
(
public abstract int interpret (Context con);
)

/ / End of expression at the role of

class Constant extends Expression
(
private int i;
public Constant (int i)
(
this.i = i;
)

public int interpret (Context con)
(
return i;
)
)

class Variable extends Expression
(
public int interpret (Context con)
(
/ / this call to interpret for the Variable object methods
return con.LookupValue (this);
)
)

/ / The role of non-terminator expression

class Add extends Expression
(
private Expression left, right;
public Add (Expression left, Expression right)
(
this.left = left;
this.right = right;
)

public int interpret (Context con)
(
return left.interpret (con) + right.interpret (con);
)
)

class Subtract extends Expression
(
private Expression left, right;
public Subtract (Expression left, Expression right)
(
this.left = left;
this.right = right;
)

public int interpret (Context con)
(
return left.interpret (con) - right.interpret (con);
)

)

class Multiply extends Expression
(
private Expression left, right;
public Multiply (Expression left, Expression right)
(
this.left = left;
this.right = right;
)
public int interpret (Context con)
(
return left.interpret (con) * right.interpret (con);
)
)

class Division extends Expression
(
private Expression left, right;
public Division (Expression left, Expression right)
(
this.left = left;
this.right = right;
)

public int interpret (Context con)
(
try (
return left.interpret (con) / right.interpret (con);
) catch (ArithmeticException ae)
(
System.out.println ( "dividend is 0!");
return -11111;
)
)
)

/ / Test procedures, calculation of (a * b) / (a-b +2)

public class Test
(
private static Expression ex;
private static Context con;
public static void main (String [] args)
(
con = new Context ();
/ / Set variables, constants
Variable a = new Variable ();
Variable b = new Variable ();
Constant c = new Constant (2);
/ / For variable assignment
con.addValue (a, 5);
con.addValue (b, 7);
/ / Computation of the sentence structure from our own analysis, construction
ex = new Division (new Multiply (a, b), new Add (new Subtract (a, b), c));
System.out.println ( "computing results for:" + ex.interpret (con));
)
)

Interpreter mode and did not specify how to create an abstract syntax tree, so it can be varied to achieve, in the above we are directly provided in the Test and, of course, better, more professional ways.

For at the end, GOF recommended Flyweight pattern to share their copy, because they are repeated several times. However, taking into account the element pattern to enjoy the use of limitations, or when I suggest that you repeat at the end of the system sufficient time to consider more than Flyweight pattern.

Fourth, the advantages and disadvantages of

Interpreter pattern provides a simple way to implement the syntax and grammar easily modified or extended. Many of the general type of system to use a similar syntax, you can use to replace an interpreter for each realization of a rules interpreter. And interpreter of the rules are different for different categories to achieve, so that makes adding a new simple grammar rules.

But the interpreter for the model difficult to maintain the complexity of grammar. Imagine, each corresponding to a rule to a class, and these types of expression but also the role of abstract recursive calls, and more of the categories such as intertwinedhow horrible thing ah!

V. GENERAL

This mode of interpretation should have some general awareness of it, as the case of this model the lack of use, so most of the points in this article directly from the original GOF. Only examples of the code is adopted to achieve and debugging of.


Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.