#include <iostream>
#include <memory>
#include <string>
class Image {
public:
virtual void display() = 0;
virtual ~Image() = default;
};
class RealImage : public Image {
std::string filename;
void loadFromDisk() {
std::cout << "Loading image from disk: " << filename << std::endl;
}
public:
explicit RealImage(const std::string& file) : filename(file) {
loadFromDisk();
}
void display() override {
std::cout << "Displaying image: " << filename << std::endl;
}
};
class ProxyImage : public Image {
std::string filename;
std::unique_ptr<RealImage> realImage;
public:
explicit ProxyImage(const std::string& file) : filename(file) {}
void display() override {
if (!realImage) {
realImage = std::make_unique<RealImage>(filename);
}
realImage->display();
}
};
int main() {
ProxyImage proxy("photo.jpg");
std::cout << "Proxy created, image not loaded yet.\n" << std::endl;
std::cout << "First call to display():" << std::endl;
proxy.display();
std::cout << "\nSecond call to display():" << std::endl;
proxy.display();
return 0;
}