QtDesignPatterns/interpreterpattern/interface.h

66 lines
1.3 KiB
C
Raw Normal View History

2024-02-06 17:21:37 +08:00
#include <string>
#include <iostream>
using namespace std;
class Expression
{
public:
Expression(){}
virtual ~Expression(){}
virtual bool interpret(string context) = 0;
};
class TerminalExpression: public Expression
{
public:
TerminalExpression(string data) : data(data)
{
cout << "init TerminalExpression" << endl;
}
bool interpret(string context)
{
if(context.find(data) != string::npos)
{
return true;
}
return false;
}
private:
string data;
};
class OrExpression: public Expression
{
public:
OrExpression(Expression *exp1, Expression *exp2) : exp1(exp1), exp2(exp2)
{
cout << "init OrExpression" << endl;
}
bool interpret(string context)
{
return exp1->interpret(context) || exp2->interpret(context);
}
private:
Expression *exp1 = nullptr;
Expression *exp2 = nullptr;
};
class AndExpression: public Expression
{
public:
AndExpression(Expression *exp1, Expression *exp2) : exp1(exp1), exp2(exp2)
{
cout << "init AndExpression" << endl;
}
bool interpret(string context)
{
return exp1->interpret(context) && exp2->interpret(context);
}
private:
Expression *exp1 = nullptr;
Expression *exp2 = nullptr;
};