|
5 | 5 |
|
6 | 6 | - **创建型模式**:[单例模式](https://github.com/youlookwhat/DesignPattern#3-单例设计模式)、[抽象工厂模式](https://github.com/youlookwhat/DesignPattern#2-工厂模式)、[建造者模式](https://github.com/youlookwhat/DesignPattern#11-建造者模式)、[工厂模式](https://github.com/youlookwhat/DesignPattern#2-工厂模式)、[原型模式](https://github.com/youlookwhat/DesignPattern#12-原型模式)。
|
7 | 7 | - **结构型模式**:[适配器模式](https://github.com/youlookwhat/DesignPattern#5-适配器模式)、[桥接模式](https://github.com/youlookwhat/DesignPattern#15-桥接模式)、[装饰模式](https://github.com/youlookwhat/DesignPattern#7-装饰者模式)、[组合模式](https://github.com/youlookwhat/DesignPattern#16-组合模式)、[外观模式](https://github.com/youlookwhat/DesignPattern#8-外观模式)、[享元模式](https://github.com/youlookwhat/DesignPattern#13-享元模式)、[代理模式](https://github.com/youlookwhat/DesignPattern#14-代理模式)。
|
8 |
| - - **行为型模式**:[模版方法模式](https://github.com/youlookwhat/DesignPattern#9-模板方法模式)、[命令模式](https://github.com/youlookwhat/DesignPattern#6-命令模式)、[迭代器模式](https://github.com/youlookwhat/DesignPattern#17-迭代器模式)、[观察者模式](https://github.com/youlookwhat/DesignPattern#1-观察者模式)、[中介者模式](https://github.com/youlookwhat/DesignPattern#18-中介者模式)、[备忘录模式](https://github.com/youlookwhat/DesignPattern#19-备忘录模式)、解释器模式、[状态模式](https://github.com/youlookwhat/DesignPattern#10-状态模式)、[策略模式](https://github.com/youlookwhat/DesignPattern#4-策略模式)、职责链模式(责任链模式)、访问者模式。 |
| 8 | + - **行为型模式**:[模版方法模式](https://github.com/youlookwhat/DesignPattern#9-模板方法模式)、[命令模式](https://github.com/youlookwhat/DesignPattern#6-命令模式)、[迭代器模式](https://github.com/youlookwhat/DesignPattern#17-迭代器模式)、[观察者模式](https://github.com/youlookwhat/DesignPattern#1-观察者模式)、[中介者模式](https://github.com/youlookwhat/DesignPattern#18-中介者模式)、[备忘录模式](https://github.com/youlookwhat/DesignPattern#19-备忘录模式)、[解释器模式](https://github.com/youlookwhat/DesignPattern#20-解释器模式)、[状态模式](https://github.com/youlookwhat/DesignPattern#10-状态模式)、[策略模式](https://github.com/youlookwhat/DesignPattern#4-策略模式)、职责链模式(责任链模式)、访问者模式。 |
9 | 9 |
|
10 | 10 | > 参照Hongyang、菜鸟教程、极客学院等处文章所写。如有错误欢迎指正,如有侵权,请联系我删除。
|
11 | 11 |
|
|
52 | 52 |
|
53 | 53 | - 19.[ 设计模式 备忘录模式(Memento Pattern) 以使用备忘录为例](https://www.runoob.com/design-pattern/memento-pattern.html)
|
54 | 54 |
|
| 55 | + - 20.[ 设计模式 解释器模式(Interpreter Pattern) 以解释一句话为例](https://www.runoob.com/design-pattern/interpreter-pattern.html) |
| 56 | + |
55 | 57 |
|
56 | 58 | ## Source Code
|
57 | 59 | > - 1. [Observer](https://github.com/youlookwhat/DesignPattern/tree/master/app/src/main/java/com/example/jingbin/designpattern/observer)
|
|
73 | 75 | > - 17. [Iterator](https://github.com/youlookwhat/DesignPattern/tree/master/app/src/main/java/com/example/jingbin/designpattern/iterator)
|
74 | 76 | > - 18. [Mediator](https://github.com/youlookwhat/DesignPattern/tree/master/app/src/main/java/com/example/jingbin/designpattern/mediator)
|
75 | 77 | > - 19. [Memento](https://github.com/youlookwhat/DesignPattern/tree/master/app/src/main/java/com/example/jingbin/designpattern/memento)
|
| 78 | +> - 20. [Interpreter](https://github.com/youlookwhat/DesignPattern/tree/master/app/src/main/java/com/example/jingbin/designpattern/interpreter) |
76 | 79 |
|
77 | 80 | ## Project Picture
|
78 | 81 |
|
|
1190 | 1193 | */
|
1191 | 1194 | ```
|
1192 | 1195 |
|
| 1196 | +### 20. 解释器模式 |
| 1197 | +> 提供了评估语言的语法或表达式的方式,它属于行为型模式。这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。 |
| 1198 | +
|
| 1199 | + - **主要解决**:对于一些固定文法构建一个解释句子的解释器。 |
| 1200 | + |
| 1201 | +最小单元步骤: |
| 1202 | + |
| 1203 | + - 1、创建一个表达式接口 Expression。 |
| 1204 | + |
| 1205 | + ```java |
| 1206 | + public interface Expression { |
| 1207 | + public boolean interpreter(String content); |
| 1208 | + } |
| 1209 | + ``` |
| 1210 | + |
| 1211 | + - 2、创建实现了上述接口的实体类。TerminalExpression、OrExpression、AndExpression。 |
| 1212 | + |
| 1213 | + ```java |
| 1214 | + public class TerminalExpression implements Expression { |
| 1215 | + |
| 1216 | + private String data; |
| 1217 | + |
| 1218 | + public TerminalExpression(String data) { |
| 1219 | + this.data = data; |
| 1220 | + } |
| 1221 | + |
| 1222 | + @Override |
| 1223 | + public boolean interpreter(String content) { |
| 1224 | + // 是包含判断 |
| 1225 | + return content.contains(data); |
| 1226 | + } |
| 1227 | + } |
| 1228 | + ``` |
| 1229 | + |
| 1230 | + ```java |
| 1231 | + public class OrExpression implements Expression { |
| 1232 | + |
| 1233 | + private Expression expression1; |
| 1234 | + private Expression expression2; |
| 1235 | + |
| 1236 | + public OrExpression(Expression expression1, Expression expression2) { |
| 1237 | + this.expression1 = expression1; |
| 1238 | + this.expression2 = expression2; |
| 1239 | + } |
| 1240 | + |
| 1241 | + @Override |
| 1242 | + public boolean interpreter(String content) { |
| 1243 | + return expression1.interpreter(content) || expression2.interpreter(content); |
| 1244 | + } |
| 1245 | + } |
| 1246 | + ``` |
| 1247 | + |
| 1248 | + ```java |
| 1249 | + public class AndExpression implements Expression { |
| 1250 | + |
| 1251 | + private Expression expression1; |
| 1252 | + private Expression expression2; |
| 1253 | + |
| 1254 | + public AndExpression(Expression expression1, Expression expression2) { |
| 1255 | + this.expression1 = expression1; |
| 1256 | + this.expression2 = expression2; |
| 1257 | + } |
| 1258 | + |
| 1259 | + @Override |
| 1260 | + public boolean interpreter(String content) { |
| 1261 | + return expression1.interpreter(content) && expression2.interpreter(content); |
| 1262 | + } |
| 1263 | + } |
| 1264 | + ``` |
| 1265 | + |
| 1266 | + - 3、使用 Expression 类来创建规则,并解析它们。 |
| 1267 | + |
| 1268 | + ```java |
| 1269 | + /** |
| 1270 | + * 规则:jingbin 和 youlookwhat 是男性 |
| 1271 | + */ |
| 1272 | + public static Expression getMaleExpression() { |
| 1273 | + TerminalExpression jingbin = new TerminalExpression("jingbin"); |
| 1274 | + TerminalExpression youlookwhat = new TerminalExpression("youlookwhat"); |
| 1275 | + return new OrExpression(jingbin, youlookwhat); |
| 1276 | + } |
| 1277 | + |
| 1278 | + /** |
| 1279 | + * 规则:Julie 是一个已婚的女性 |
| 1280 | + */ |
| 1281 | + public static Expression getMarriedWomanExpression() { |
| 1282 | + TerminalExpression julie = new TerminalExpression("Julie"); |
| 1283 | + TerminalExpression married = new TerminalExpression("Married"); |
| 1284 | + return new AndExpression(julie, married); |
| 1285 | + } |
| 1286 | + |
| 1287 | + Expression maleExpression = getMaleExpression(); |
| 1288 | + // jingbin is male: true |
| 1289 | + Log.e("---", "jingbin is male: " + maleExpression.interpreter("jingbin")); |
| 1290 | + |
| 1291 | + Expression womanExpression = getMarriedWomanExpression(); |
| 1292 | + // Julie is married woman: true |
| 1293 | + Log.e("---", "Julie is married woman: " + womanExpression.interpreter("Married Julie")); |
| 1294 | + |
| 1295 | + ``` |
| 1296 | + |
| 1297 | + |
1193 | 1298 | ## Download
|
1194 | 1299 | - [DesignPattern.apk](http://download.csdn.net/detail/jingbin_/9684545)
|
1195 | 1300 |
|
|
0 commit comments