File tree Expand file tree Collapse file tree 5 files changed +79
-0
lines changed
java-des/src/com/java/design/interpreter Expand file tree Collapse file tree 5 files changed +79
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .java .design .interpreter ;
2
+
3
+ public class AdvanceExpression extends Expression {
4
+
5
+ @ Override
6
+ void interpret () {
7
+
8
+ System .out .println ("这是一个高级解析器 ..." );
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ package com .java .design .interpreter ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ public class Context {
7
+
8
+ private String content ;
9
+
10
+ private List <Expression > expressionList = new ArrayList <Expression >();
11
+
12
+ public String getContent () {
13
+ return content ;
14
+ }
15
+
16
+ public void setContent (String content ) {
17
+ this .content = content ;
18
+ }
19
+
20
+ public void add (Expression expression ) {
21
+ expressionList .add (expression );
22
+ }
23
+
24
+ public List <Expression > getList () {
25
+ return expressionList ;
26
+ }
27
+
28
+ }
Original file line number Diff line number Diff line change
1
+ package com .java .design .interpreter ;
2
+
3
+ public abstract class Expression {
4
+
5
+ abstract void interpret ();
6
+ }
Original file line number Diff line number Diff line change
1
+ package com .java .design .interpreter ;
2
+
3
+ /**
4
+ * 解释器模式 -----> 是一种用得比较少的行为型模式.提供了一种解释语言的语法或表达式的方式. 通过定义一个表达式接口,解释一个特定的上下文.
5
+ *
6
+ * @author Administrator
7
+ *
8
+ */
9
+ public class InterpreterPattern {
10
+
11
+ public static void main (String [] args ) {
12
+
13
+ Context context = new Context ();
14
+ context .add (new SimpleExpression ());
15
+ context .add (new SimpleExpression ());
16
+ context .add (new AdvanceExpression ());
17
+ context .add (new SimpleExpression ());
18
+
19
+ for (Expression ex : context .getList ()) {
20
+
21
+ ex .interpret ();
22
+ }
23
+ }
24
+
25
+ }
Original file line number Diff line number Diff line change
1
+ package com .java .design .interpreter ;
2
+
3
+ public class SimpleExpression extends Expression {
4
+
5
+ @ Override
6
+ void interpret () {
7
+
8
+ System .out .println ("这是一个简单的解析器 ..." );
9
+ }
10
+ }
You can’t perform that action at this time.
0 commit comments