理解:可以广义的理解为创造一种语言,实现该语言的解释器,然后用创造的语言编写程序

对比:如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模式的更多相关文章

  1. Java设计模式(17)解释器模式(Interpreter模式)

    Interpreter定义:定义语言的文法,并且建立一个解释器来解释该语言中的句子. Interpreter似乎使用面不是很广,它描述了一个语言解释器是如何构成的,在实际应用中,我们可能很少去构造一个 ...

  2. 设计模式(二十三)Interpreter模式

    在Interpreter模式中,程序要解决的问题会被用非常简单的“迷你语言”表述出来,即用“迷你语言”编写的“迷你程序”把具体的问题表述出来.迷你程序是无法单独工作的,还需要用java语言编写一个负责 ...

  3. linkin大话设计模式--常用模式总结

    linkin大话设计模式--常用模式总结 一,常用设计模式定义 Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. Adapter( ...

  4. Java设计模式----解释器模式

    计算器中,我们输入“20 + 10 - 5”,计算器会得出结果25并返回给我们.可你有没有想过计算器是怎样完成四则运算的?或者说,计算器是怎样识别你输入的这串字符串信息,并加以解析,然后执行之,得出结 ...

  5. .NET设计模式访问者模式

    一.访问者模式的定义: 表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 二.访问者模式的结构和角色: 1.Visitor 抽象访问者角色,为该 ...

  6. [Head First设计模式]饺子馆(冬至)中的设计模式——工厂模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  7. [Head First设计模式]抢票中的设计模式——代理模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  8. [Head First设计模式]策略模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  9. [Head First设计模式]餐馆中的设计模式——命令模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  10. [Head First设计模式]生活中学设计模式——迭代器模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

随机推荐

  1. 138 张图带你 MySQL 入门

    SQL 基础使用 MySQL 是一种关系型数据库,说到关系,那么就离不开表与表之间的关系,而最能体现这种关系的其实就是我们接下来需要介绍的主角 SQL,SQL 的全称是 Structure Query ...

  2. Java WebService _CXF、Xfire、AXIS2、AXIS1_四种发布方式(优缺点对比)

    xis,axis2,Xfire以及cxf对比 http://ws.apache.org/axis/ http://axis.apache.org/axis2/java/core/ http://xfi ...

  3. SSTI-服务端模板注入漏洞

      原理: 服务端模板注入是由于服务端接收了用户的输入,将其作为 Web 应用模板内容的一部分,在进行目标编译渲染的过程中,执行了用户插入的恶意内容,因而导致了敏感信息泄露.代码执行.GetShell ...

  4. activiti学习笔记一

    activiti学习笔记 在讲activiti之前我们必须先了解一下什么是工作流,什么是工作流引擎. 在我们的日常工作中,我们会碰到很多流程化的东西,什么是流程化呢,其实通俗来讲就是有一系列固定的步骤 ...

  5. 设计模式系列之代理模式(Proxy Pattern)——对象的间接访问

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  6. vue全家桶(2.6)

    3.9.滚动行为 设置滚动行为的作用是导航到新路由时,让页面滚动到你想要的位置. const router = new VueRouter({ routes: [...], scrollBehavio ...

  7. mysql 主键自增设置,插入数据就不必再设置了。

    (完)

  8. 如何查看docker run启动参数命令

    通过runlike去查看一个容器的docker run启动参数 安装pip yum install -y python-pip 安装runlike pip install runlike 查看dock ...

  9. 神秘的角落之张东升做了Java老师,悲剧就这样开始了

    故事背景   主人公张东升是某大学某软件学院的一名Java老师,他平时给学生讲课风格古怪呆板,加上他普通话不标准,一口家乡话,每次给学生讲课都分两种情况: 第一种情况:手持课本,把本节要讲的内容按书本 ...

  10. sublime 搜索时忽略文件夹

    如上图:添加 "folder_exclude_patterns": ["要忽略的文件夹"]