Patterns / Structural patterns
image

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

//Subject 
class Graphic {
protected:
    string fileName;
public:
    virtual void draw() = 0;

    string getFileName() {
        return fileName;
    }
};

//RealSubject
class Imagepublic Graphic {
public:
    Image(string fileName) {
        this->fileName = fileName;
    }

    //Request()
    void draw() {
        cout << "draw " + fileName + "\n";
    }
};

//Proxy
class ImageProxypublic Graphic {
private:
    Image *image = NULL;

    Image getImage() {
        if (image == NULL) {
            image = new Image(fileName);
        }
        return *image;
    }

public:
    ImageProxy(string fileName) {
        this->fileName = fileName;
    }

    void draw() {
        getImage().draw();
    }
};

//Client
ImageProxy proxy("1.png");
//operation without creating a RealSubject
string fileName = proxy.getFileName();
//forwarded to the RealSubject
proxy.draw();

cout << "fileName: " << fileName;


Provide a surrogate or placeholder for another object to control access to it. The proxy implements the interface of the original object.