#include <iostream>
#include <string.h>
//interpreter解释器模式。
//一个解释类对封装的行为进行公共需求解释的一种方式。
using namespace std;
class Contex
{
public:
Contex(string inputstr)
{
InterpreterStr = inputstr;
}
private:
string InterpreterStr;
friend class Son1;
friend class Son2;
};
class Base
{
public:
virtual void Interpreter(Contex &co) = 0;
};
class Son1 : public Base
{
public:
void Interpreter(Contex &co)
{
cout <<"Son1::"<< co.InterpreterStr.c_str() << endl;
}
};
class Son2 : public Base
{
public:
void Interpreter(Contex &co)
{
cout <<"Son2::"<< co.InterpreterStr.c_str() << endl;
}
};
int main()
{
Contex co("hello word!!!");
Son1 *son1= new Son1();
Son2 *son2= new Son2();
son1->Interpreter(co);
son2->Interpreter(co);
return 0;
}
设计模式:解释者模式
最新推荐文章于 2021-07-19 19:18:25 发布