设计模式:interpreter模式
理解:可以广义的理解为创造一种语言,实现该语言的解释器,然后用创造的语言编写程序
对比:如xml就是一种语言,解析xml的代码就是解释器
例子:
//目标:定义4中几种命令,使用C++解析
//如下:
//command go end
//command back end
//command right end
//command left end
//repeat 4 go back end
//command left left left end
class Command
{
public:
virtual void excute() = 0;
}; class GoCommand: public Command
{
public:
virtual void excute()
{
cout << "Go";
}
}; class BackCommand: public Command
{
public:
virtual void excute()
{
cout << "Back";
}
}; class LeftCommand: public Command
{
public:
virtual void excute()
{
cout << "Left";
}
}; class RightCommand: public Command
{
public:
virtual void excute()
{
cout << "Right";
}
}; class CommandList: public Command
{
protected:
vector<Command*> v;
public:
virtual void excute()
{
for(vector<Command*>::iterator it=v.begin(); it!=v.end(); ++it)
{
cout << " ";
(*it)->excute();
cout << endl;
}
} void addCommand(Command* command)
{
v.push_back(command);
} ~CommandList()
{
for(vector<Command*>::iterator it=v.begin(); it!=v.end(); ++it)
{
delete (*it);
}
}
}; class RepeatCommand: public CommandList
{
int repeat;
public:
RepeatCommand(int repeat)
{
this->repeat = repeat;
} virtual void excute()
{
for(int i=0; i<repeat; i++)
{
for(vector<Command*>::iterator it=v.begin(); it!=v.end(); ++it)
{
cout << " ";
(*it)->excute();
}
}
}
};
CommandList *program = NULL; void SplitString(const string& s, vector<string>& v, const string& c)
{
string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while(string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
return;
} void ParseLine(vector<string>& v, CommandList* commandList)
{
if(v.front() == string("command"))
{
if(v.back() == string("end"))
{
vector<string> sub(v);
sub.erase(sub.begin());
sub.erase(sub.end());
ParseLine(sub, commandList);
}
else
{
cout << "Parse error1" << endl;
} }
else if(v.front() == string("repeat"))
{
if(v.back() == string("end"))
{
vector<string> sub(v);
sub.erase(sub.begin());
sub.erase(sub.end()); istringstream is(sub[0]);
int i = 0;
is >> i; RepeatCommand* reptCmd = new RepeatCommand(i);
commandList->addCommand(reptCmd); sub.erase(sub.begin());
ParseLine(sub, reptCmd); }
else
{
cout << "Parse error1" << endl;
}
}
else if(v.front() == string("go"))
{
commandList->addCommand(new GoCommand);
vector<string> sub(v);
sub.erase(sub.begin());
if(sub.size() > 0)
{
ParseLine(sub, commandList);
}
}
else if(v.front() == string("back"))
{
commandList->addCommand(new BackCommand);
vector<string> sub(v);
sub.erase(sub.begin());
if(sub.size() > 0)
{
ParseLine(sub, commandList);
}
}
else if(v.front() == string("left"))
{
commandList->addCommand(new LeftCommand);
vector<string> sub(v);
sub.erase(sub.begin());
if(sub.size() > 0)
{
ParseLine(sub, commandList);
}
}
else if(v.front() == string("right"))
{
commandList->addCommand(new RightCommand);
vector<string> sub(v);
sub.erase(sub.begin());
if(sub.size() > 0)
{
ParseLine(sub, commandList);
}
}
else
{
cout << "Parse error0" << endl;
}
} void Parse(string name)
{
program = new CommandList(); ifstream infile(name.c_str());
string cmd;
vector<string> v;
while(getline(infile, cmd))
{
v.clear();
SplitString(cmd, v, " ");
ParseLine(v, program);
}
infile.close();
}
int main()
{
Parse(string("abc.txt"));
program->excute(); return 0;
}
设计模式:interpreter模式的更多相关文章
- Java设计模式(17)解释器模式(Interpreter模式)
Interpreter定义:定义语言的文法,并且建立一个解释器来解释该语言中的句子. Interpreter似乎使用面不是很广,它描述了一个语言解释器是如何构成的,在实际应用中,我们可能很少去构造一个 ...
- 设计模式(二十三)Interpreter模式
在Interpreter模式中,程序要解决的问题会被用非常简单的“迷你语言”表述出来,即用“迷你语言”编写的“迷你程序”把具体的问题表述出来.迷你程序是无法单独工作的,还需要用java语言编写一个负责 ...
- linkin大话设计模式--常用模式总结
linkin大话设计模式--常用模式总结 一,常用设计模式定义 Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. Adapter( ...
- Java设计模式----解释器模式
计算器中,我们输入“20 + 10 - 5”,计算器会得出结果25并返回给我们.可你有没有想过计算器是怎样完成四则运算的?或者说,计算器是怎样识别你输入的这串字符串信息,并加以解析,然后执行之,得出结 ...
- .NET设计模式访问者模式
一.访问者模式的定义: 表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 二.访问者模式的结构和角色: 1.Visitor 抽象访问者角色,为该 ...
- [Head First设计模式]饺子馆(冬至)中的设计模式——工厂模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- [Head First设计模式]抢票中的设计模式——代理模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- [Head First设计模式]策略模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- [Head First设计模式]餐馆中的设计模式——命令模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- [Head First设计模式]生活中学设计模式——迭代器模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
随机推荐
- elk4
传统问题: 微服务系统下服务器数量过大,如果还在使用依次登录每台机器的传统方法查询日志,这样效率非常低下.ELK 是elastic公司提供的一套完整的日志收集以及展示的解决方案,是三个产品的首字母缩写 ...
- struct2面试准备
二 工作流程1.客户端浏览器发出HTTP请求.2.根据web.xml配置,该请求被FilterDispatcher接收3.根据struts.xml配置,找到需要调用的Action类和方法, 并通过Io ...
- JavaScript图形实例:图形放大镜效果
1. 基本四瓣花型图案 根据四瓣花卉线的参数方程: t= r*(1+sin(12*θ)/5)*(0.5+sin(4*θ)/2); x=t*cos(θ)); y=t*sin(θ)); 编写如下的HTML ...
- 入门大数据---Flink核心概念综述
一.Flink 简介 Apache Flink 诞生于柏林工业大学的一个研究性项目,原名 StratoSphere .2014 年,由 StratoSphere 项目孵化出 Flink,并于同年捐赠 ...
- 入门大数据---Sqoop简介与安装
一.Sqoop 简介 Sqoop 是一个常用的数据迁移工具,主要用于在不同存储系统之间实现数据的导入与导出: 导入数据:从 MySQL,Oracle 等关系型数据库中导入数据到 HDFS.Hive.H ...
- JDK8--04:内置接口
在JDK8--3中已经说过,使用lambda方法需要新增函数式接口,为了使用方便,JDK8已经提供了许多内置接口,总的归纳来说,有四大函数式接口. /** * * java8 四大内置接口 * * 1 ...
- 【Flutter实战】自定义滚动条
老孟导读:[Flutter实战]系列文章地址:http://laomengit.com/guide/introduction/mobile_system.html 默认情况下,Flutter 的滚动组 ...
- SpringBoot2.x的依赖管理
前提 这篇文章是<SpringBoot2.x入门>专辑的第1篇文章,使用的SpringBoot版本为2.3.1.RELEASE,JDK版本为1.8. 主要梳理一下SpringBoot2.x ...
- JavaWeb项目在浏览器点击几次就阻塞了
问题描述 在学习JavaWeb项目时,通过IDE启动项目后,在浏览器点击几次页面中的链接就阻塞了,浏览器一直转圈圈无法加载,后台日志也没有输出. 第一次遇见这种情况,没有日志完全无法分析到底是什么问题 ...
- pycham中报:ModuleNotFoundError: No module named 'pymysql'
参考https://www.cnblogs.com/wupeiqi/articles/5713330.html https://pypi.python.org/pypi # D:\Program Fi ...