解释器模式(Interpreter)的代码 C++版
来源:互联网 发布:蝶々世界动作数据 编辑:程序博客网 时间:2024/05/21 13:15
《设计模式》原书中已对解析器模式讲得很清楚,只是例子代码不够完整,我把原书代码code下来,并添加了一点必要的内容,以便通过编译
以下是Interpreter模式代码:已在vc7.1编译验证
#include <iostream>
#include <map>
#include <string>
#include <utility>
using namespace std;
class VariableExp;
class Context
{
public:
typedef std::pair<std::string, bool> str_bool;
bool Lookup(const char* name) const
{
return m_varMap.find(name)->second;
}
void Assign(VariableExp* ve, bool b);
private:
std::map<std::string, bool> m_varMap;
};
class BooleanExp
{
public:
BooleanExp(){}
virtual ~BooleanExp(){}
virtual bool Evaluate(Context&)=0;
virtual BooleanExp* Replace(const char*, BooleanExp&)=0;
virtual BooleanExp* Copy() const = 0;
protected:
private:
};
class VariableExp : public BooleanExp
{
public:
VariableExp(const char* name)
{
m_name = strdup(name);
}
bool Evaluate(Context& aContext)
{
return aContext.Lookup(m_name);
}
virtual BooleanExp* Copy() const
{
return new VariableExp(m_name);
}
virtual BooleanExp* Replace(const char* name, BooleanExp& exp)
{
if(strcmp(name, m_name)==0){
return exp.Copy();
}
else{
return new VariableExp(m_name);
}
}
char* m_name;
};
class AndExp : public BooleanExp
{
public:
AndExp(BooleanExp* op1, BooleanExp* op2)
{
m_operand1 = op1;
m_operand2 = op2;
}
virtual ~AndExp(){}
bool Evaluate(Context& aContext)
{
return m_operand1->Evaluate(aContext) && m_operand2->Evaluate(aContext);
}
virtual BooleanExp* Copy() const
{
return new AndExp(m_operand1->Copy(), m_operand2->Copy());
}
virtual BooleanExp* Replace(const char* name, BooleanExp& exp)
{
return new AndExp(m_operand1->Replace(name, exp),
m_operand2->Replace(name, exp));
}
private:
BooleanExp* m_operand1;
BooleanExp* m_operand2;
};
class OrExp : public BooleanExp
{
public:
OrExp(BooleanExp* op1, BooleanExp* op2)
{
m_operand1 = op1;
m_operand2 = op2;
}
virtual ~OrExp(){}
bool Evaluate(Context& aContext)
{
return m_operand1->Evaluate(aContext) || m_operand2->Evaluate(aContext);
}
virtual BooleanExp* Copy() const
{
return new OrExp(m_operand1->Copy(), m_operand2->Copy());
}
virtual BooleanExp* Replace(const char* name, BooleanExp& exp)
{
return new OrExp(m_operand1->Replace(name, exp),
m_operand2->Replace(name, exp));
}
private:
BooleanExp* m_operand1;
BooleanExp* m_operand2;
};
class NotExp : public BooleanExp
{
public:
NotExp(BooleanExp* op)
{
m_operand = op;
}
virtual ~NotExp(){}
bool Evaluate(Context& aContext)
{
return !m_operand->Evaluate(aContext);
}
virtual BooleanExp* Copy() const
{
return new NotExp(m_operand->Copy());
}
virtual BooleanExp* Replace(const char* name, BooleanExp& exp)
{
return new NotExp(m_operand->Replace(name, exp));
}
private:
BooleanExp* m_operand;
};
class Constant : public BooleanExp
{
public:
Constant(bool b)
{
m_bConstant = b;
}
virtual ~Constant(){}
bool Evaluate(Context& aContext)
{
return m_bConstant;
}
virtual BooleanExp* Copy() const
{
return new Constant(m_bConstant);
}
virtual BooleanExp* Replace(const char* name, BooleanExp& exp)
{
return 0;
}
private:
bool m_bConstant;
};
int main()
{
BooleanExp* expression;
Context context;
VariableExp* x = new VariableExp("X");
VariableExp* y = new VariableExp("Y");
expression = new OrExp(
new AndExp(new Constant(true), x),
new AndExp(y, new NotExp(x)));
context.Assign(x, false);
context.Assign(y, true);
bool result = expression->Evaluate(context);
cout<<"result: "<<result<<endl;
}
void Context::Assign( VariableExp* ve, bool b )
{
m_varMap.insert(str_bool(ve->m_name, b));
}
- 解释器模式(Interpreter)的代码 C++版
- 解释器(Interpreter )模式
- Interpreter解释器模式
- Interpreter 解释器模式
- Interpreter 解释器模式
- 解释器模式(Interpreter)
- interpreter 解释器模式
- 解释器模式-interpreter
- Interpreter - 解释器模式
- 解释器模式(Interpreter)
- 解释器模式 interpreter
- Interpreter解释器模式
- 原始的解释器模式(Interpreter Pattern)
- 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)
- 解释器模式(Interpreter Pattern)
- 解释器模式(Interpreter Pattern)
- 解释器模式(Interpreter)
- Interpreter(解释器模式)
- do/while(0)的妙用
- css技巧
- java解析xml文件四种方式
- Javascript中最常用的55个经典技巧
- 一键清理系统垃圾文件
- 解释器模式(Interpreter)的代码 C++版
- 每个人都活在自己的精神世界中
- Windows API一日一练(87)CreateProcess函数收藏
- javascript弹出消息提示框
- nio socket 及其开源框架MINA学习总结(一)
- ubuntu8.04 PDF中文乱码问题
- 全自动上网,提高上网效率?
- nio socket 及其开源框架MINA学习总结(二)
- 再谈用MFC实现文件拖放到编辑框