解释器模式
来源:互联网 发布:ioscf手游刷枪软件 编辑:程序博客网 时间:2024/05/22 08:19
本文转自http://blog.csdn.net/zhangerqing/article/details/8245537
23、解释器模式(Interpreter)
解释器模式是我们暂时的最后一讲,一般主要应用在OOP开发中的编译器的开发中,所以适用面比较窄。
Context类是一个上下文环境类,Plus和Minus分别是用来计算的实现,代码如下:
- public interface Expression {
- public int interpret(Context context);
- }
- public class Plus implements Expression {
- @Override
- public int interpret(Context context) {
- return context.getNum1()+context.getNum2();
- }
- }
- public class Minus implements Expression {
- @Override
- public int interpret(Context context) {
- return context.getNum1()-context.getNum2();
- }
- }
- public class Context {
- private int num1;
- private int num2;
- public Context(int num1, int num2) {
- this.num1 = num1;
- this.num2 = num2;
- }
- public int getNum1() {
- return num1;
- }
- public void setNum1(int num1) {
- this.num1 = num1;
- }
- public int getNum2() {
- return num2;
- }
- public void setNum2(int num2) {
- this.num2 = num2;
- }
- }
- public class Test {
- public static void main(String[] args) {
- // 计算9+2-8的值
- int result = new Minus().interpret((new Context(new Plus()
- .interpret(new Context(9, 2)), 8)));
- System.out.println(result);
- }
- }
基本就这样,解释器模式用来做各种各样的解释器,如正则表达式等的解释器等等!
0 0
- 深入浅出解释器模式
- 解释器(Interpreter )模式
- Interpreter解释器模式
- Interperter 解释器模式
- 解释器模式 InterpretePattern
- 解释器模式
- 解释器模式
- 深入浅出解释器模式
- 解释器模式(python)
- 解释器模式
- 解释器模式 详解
- 解释器模式
- Java解释器模式
- 解释器模式
- Interpreter 解释器模式
- java 解释器模式
- 解释器模式
- 解释器模式
- python常用代码和函数实例
- 寻找最好的编程语言
- OBIEE去掉主页左边部分内容
- [LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)
- spirntf,你知道多少?
- 解释器模式
- spring源码研究之IoC容器在web容器中初始化过程
- html 无序列表_有序列表_框架
- ShellExecute, WinExec, CreateProcess区别
- canvas绘图 echarts 基本使用
- Unable to find vcvarsall.bat
- 嵌入式 poll函数常识
- java json转map方法
- UITouch id<UIResponder>