Wzorzec udostępnia na zewnątrz klasę zastępczą, która izoluje obiekty realizujące żądane operacje. Wywołanie funkcji klasy zastępczej realizowane jest przez oddelegowanie tego wywołania do właściwej klasy implementującej dane działanie.

#include <iostream>

class CAbstract
{
public:
    virtual void f() = 0;
    virtual void g() = 0;

    virtual ~CAbstract() {}
};

class CImplementation : public CAbstract
{
public:
    void f() { std::cout << "CImplementation.f()" << std::endl; }
    void g() { std::cout << "CImplementation.g()" << std::endl; }
};

class CProxy : public CAbstract
{
    CAbstract* m_pImplementation;

public:
    CProxy() { m_pImplementation = new CImplementation(); }
   ~CProxy() { delete m_pImplementation; }

    // Oddelegowanie wywolan do implementacji
    void f()
    {
        if (m_pImplementation)
            m_pImplementation->f();
    }
    void g()
    {
        if (m_pImplementation)
            m_pImplementation->g();
    }
};

int main(int nArguments, char* szArguments[])
{
    CProxy p;
    p.f();
    p.g();

    return 0;
}

0 Komentarzy

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *