본문 바로가기

카테고리 없음

[Design Pattern] Decorator 예제

출처는 위에 적혀 있습니다. 연습용 예제 입니다.

// Simple decorator pattern example
// (c) Daniel Livingstone 2012
// CC-BY-SA

#include <string>
#include <iostream>
using namespace std;

class AbstractNPC {
public:
    virtual void render() = 0;
};

class NPC : public AbstractNPC {
private:
    string name;
public:
    NPC(string basename) { name.assign(basename); }
    NPC(char * basename) { name.assign(basename); }
    void render() { cout << name; }
};

class NPCDecorator : public AbstractNPC {
private:
    AbstractNPC * npc;
public:
    NPCDecorator(AbstractNPC *n) { npc = n; }
    void render() { npc->render(); } // delegate render to npc data member
};

class Elite : public NPCDecorator {
public:
    Elite(AbstractNPC *n) : NPCDecorator(n) { }
    void render() {
        cout << "Elite "; // render special features
        NPCDecorator::render(); // delegate to base class
    }
};

class Captain : public NPCDecorator {
public:
    Captain(AbstractNPC *n) : NPCDecorator(n) { }
    void render() {
        cout << "Captain "; // render special features
        NPCDecorator::render(); // delegate to base class
    }
};

class Shaman : public NPCDecorator {
public:
    Shaman(AbstractNPC *n) : NPCDecorator(n) { }
    void render() {
        NPCDecorator::render(); // delegate to base class
        cout << " Shaman "; // render special features *after* base
    }
};

int main(int argc, char **argv)
{
    AbstractNPC *goblin1 = new Elite(new Shaman(new NPC("Goblin")));
    AbstractNPC *orc1 = new Elite(new Captain(new NPC("Orc")));
    goblin1->render(); cout << endl;
    orc1->render(); cout << endl;

    delete goblin1;
    delete orc1;
    cin.get();
    return 0;
}