设计模式: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设计模式]山西面馆中的设计模式— ...
随机推荐
- 微信小程序scroll-view
使用竖向滚动时,需要给<scroll-view/>一个固定高度,通过 WXSS 设置 height.以下列举一个示例: scroll-top的优先级要高于scroll-into-view的 ...
- ObjectOutputStream和ObjectInputStream对对象进行序列化和反序列化
1 Java序列化和反序列化简介 Java序列化是指把对象转换为字节序列的过程,而Java反序列化是指把字节序列恢复为java对象的过程. 我们把对象序列化成有序字节流,保存到本地磁盘或者Redis等 ...
- SpringBoot -- 项目结构+启动流程
一.简述: 项目结构 二.简述:启动流程 说springboot的启动流程,当然少不了springboot启动入口类 @SpringBootApplication public class Sprin ...
- elk3
视频来自: bootstrap.memory_lock要设置为true,默认使用物理内存 mini_mum_nodes:1表示es集群中最小的matser节点数目,默认就是1,当es集群数目较少的时候 ...
- 学习oracle的SQL语句 练习
--1.查询emp表,显示薪水大于2000,且工作类别是MANAGER的雇员信息 select * from emp where sal > 2000and job = 'MANAGER'; - ...
- 入门大数据---Flink核心概念综述
一.Flink 简介 Apache Flink 诞生于柏林工业大学的一个研究性项目,原名 StratoSphere .2014 年,由 StratoSphere 项目孵化出 Flink,并于同年捐赠 ...
- python实用笔记——IO编程
打开文件 f = open('/Users/michael/test.txt', 'r') 再读取 >>> f.read() 'Hello, world!' 最后关闭 >> ...
- python冷知识(续)
python 冷知识 1.交互式中修改最大递归深度 大家都知道使用递归是有风险的,递归深度过深容易导致堆栈的溢出. 那到底,默认递归次数限制是多少呢? 可以使用sys这个库来查看 >>&g ...
- Nginx 从入门到放弃(五)
nginx的rewrite重写 nginx具有将一个路由经过加工变形成另外一个路由的功能,这就叫做重写. 重写中用到的指令 if (条件) {} 设定条件,再进行重写 set # 设定变量 retur ...
- Centos 下 Jenkins2.6 + Git + Maven Shell一件部署与备份
使用Jenkins2.6 集成Maven与Git插件做持续集成,同时编写Shell脚本备份与发布(需要稍微知道点Linux/毕竟基于Centos PS:本人Linux也是菜鸡) - 下载Jenkins ...