Skip to content

Commit 6084695

Browse files
authored
解释器模式
解释器模式
1 parent b6112b1 commit 6084695

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.java.design.interpreter;
2+
3+
public abstract class Expression {
4+
5+
abstract void interpret();
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy