Skip to content

Instantly share code, notes, and snippets.

@FONQRI
Created January 2, 2018 13:50
Show Gist options
  • Save FONQRI/416040a95784a8309131e5a9fc8624c3 to your computer and use it in GitHub Desktop.
Save FONQRI/416040a95784a8309131e5a9fc8624c3 to your computer and use it in GitHub Desktop.
C++ Decorator Design Pattern
#include <iostream>
#include <memory>
#include <string>
using namespace std;
class actor {
public:
actor();
virtual ~actor();
virtual void act();
virtual string name();
};
actor::actor() {}
void actor::act() {}
string actor::name() { return "name empty"; }
actor::~actor() {}
class actorConcretePerson : public actor {
public:
actorConcretePerson(string name);
void act();
string name();
private:
string mName;
};
actorConcretePerson::actorConcretePerson(string name) { mName = name; }
void actorConcretePerson::act() { cout << mName << " is acting" << endl; }
string actorConcretePerson::name() { return mName; }
class actorCharacter : public actor {
public:
actorCharacter(string charName);
void setActorPerson(shared_ptr<actor> anActor);
void act();
shared_ptr<actor> getActor();
void actCharacter();
string getCharName();
private:
shared_ptr<actor> mActor;
string mCharName;
};
actorCharacter::actorCharacter(string charName) { mCharName = charName; }
void actorCharacter::setActorPerson(shared_ptr<actor> anActor)
{
mActor = anActor;
}
void actorCharacter::act() { mActor->act(); }
shared_ptr<actor> actorCharacter::getActor() { return mActor; }
void actorCharacter::actCharacter()
{
act();
cout << mCharName << " is the role" << endl;
}
string actorCharacter::getCharName() { return mCharName; }
class actorCharacterTheif : public actorCharacter {
public:
actorCharacterTheif(string charName);
void setTarget(string target);
void steal();
private:
string mTarget;
};
actorCharacterTheif::actorCharacterTheif(string charName)
: actorCharacter(charName)
{
}
void actorCharacterTheif::setTarget(string target) { mTarget = target; }
void actorCharacterTheif::steal()
{
cout << getActor()->name() << ", the " << getCharName() << ": Stole "
<< mTarget << endl;
}
class actorCharacterPolice : public actorCharacter {
public:
actorCharacterPolice(string charName);
void setTrack(string track);
void catchThief();
private:
string mTrack;
};
actorCharacterPolice::actorCharacterPolice(string charName)
: actorCharacter(charName)
{
}
void actorCharacterPolice::setTrack(string track) { mTrack = track; }
void actorCharacterPolice::catchThief()
{
cout << getActor()->name() << ", the " << getCharName() << ": Cought "
<< mTrack << endl;
}
int main()
{
shared_ptr<actor> cage(new actorConcretePerson("NicolasCage"));
shared_ptr<actor> travolta(new actorConcretePerson("JohnTravolta"));
shared_ptr<actorCharacterPolice> police(
new actorCharacterPolice("Commissioner"));
shared_ptr<actorCharacterTheif> smuggler(
new actorCharacterTheif("Smuggler"));
shared_ptr<actorCharacterTheif> robber(new actorCharacterTheif("Robber"));
cout << "Basic concrete objects and methods..." << endl;
cage->act();
travolta->act();
cout << endl << "Decorator objects and methods..." << endl;
police->setActorPerson(travolta);
smuggler->setActorPerson(cage);
police->actCharacter();
police->setTrack(smuggler->getCharName());
police->catchThief();
smuggler->act();
smuggler->actCharacter();
smuggler->setTarget("Gold");
smuggler->steal();
robber->setActorPerson(travolta);
robber->act();
robber->actCharacter();
robber->setTarget("Dollars");
robber->steal();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment