QtDesignPatterns/proxypattern/interface.h

53 lines
866 B
C
Raw Permalink Normal View History

2024-02-06 17:21:37 +08:00
#include <string>
#include <iostream>
using namespace std;
class Image
{
public:
Image(){}
virtual ~Image(){}
virtual void display() = 0;
};
class RealImage : public Image
{
public:
RealImage(string fileName) : fileName(fileName)
{
loadFromDisk(fileName);
}
void loadFromDisk(string fileName)
{
cout << "Loading " + fileName << endl;
}
void display()
{
cout << "Displaying " + fileName << endl;
}
private:
string fileName;
};
class ProxyImage : public Image
{
public:
ProxyImage(string fileName) : fileName(fileName)
{
realImage = nullptr;
}
void display()
{
if (realImage == nullptr)
{
realImage = new RealImage(fileName);
}
realImage->display();
}
private:
RealImage *realImage;
string fileName;
};