Skip to content

Instantly share code, notes, and snippets.

@alexprivalov
Last active August 29, 2015 14:14
Show Gist options
  • Save alexprivalov/46143c0cac0f829dbda6 to your computer and use it in GitHub Desktop.
Save alexprivalov/46143c0cac0f829dbda6 to your computer and use it in GitHub Desktop.
class Subject
{
public:
virtual void execute() = 0;
};
class RealSubject: public Subject
{
string str;
public:
RealSubject(string s)
{
str = s;
}
/*virtual*/void execute()
{
cout << str << '\n';
}
};
class ProxySubject: public Subject
{
string first, second, third;
RealSubject *ptr;
public:
ProxySubject(string s)
{
int num = s.find_first_of(' ');
first = s.substr(0, num);
s = s.substr(num + 1);
num = s.find_first_of(' ');
second = s.substr(0, num);
s = s.substr(num + 1);
num = s.find_first_of(' ');
third = s.substr(0, num);
s = s.substr(num + 1);
ptr = new RealSubject(s);
}
~ProxySubject()
{
delete ptr;
}
RealSubject *operator->()
{
cout << first << ' ' << second << ' ';
return ptr;
}
/*virtual*/void execute()
{
cout << first << ' ' << third << ' ';
ptr->execute();
}
};
int main()
{
ProxySubject obj(string("the quick brown fox jumped over the dog"));
obj->execute();
obj.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment