#include <iostream.h>
#include <string.h>
//解释器模式
class Context //待解释的对象
{
protected:
char *temp;
public:
Context(){}
virtual ~Context(){}
public:
void SetContext(char *str)
{
temp = str;
}
char *GetText()
{
return temp;
}
};
class Explain //解释器
{
private:
Context *text;
public:
Explain(Context *text)
{this->text = text;}
virtual ~Explain(){}
public:
void Explaination()
{
char *str;
str = this->text->GetText();
int len = strlen(str);
while(1)
{
switch(*str)
{
case 'A':
cout << "a" << " ";
break;
case 'B':
cout << "2" << " ";
break;
case 'C':
cout << "R" << " ";
break;
default:
break;
}
str++;
if(--len == 0)
break;
}
cout << endl;
}
};
void main()
{
Context *text = new Context();
text->SetContext("ACBCAB");
Explain explain(text);
explain.Explaination();
}