Skip to content

Commit 33de9ea

Browse files
committed
Proxy Pattern was added.
1 parent 722abc0 commit 33de9ea

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

Structural/Proxy/Classic/C++/Main.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Subject {
5+
public:
6+
virtual void request() = 0;
7+
};
8+
9+
class RealSubject : public Subject {
10+
public:
11+
virtual void request() {
12+
cout << "Request by RealSubject" << endl;
13+
}
14+
};
15+
16+
class Proxy : public Subject {
17+
public:
18+
virtual void request() {
19+
cout << "Request by Proxy" << endl;
20+
if ( _realSubject == NULL ) {
21+
_realSubject = new RealSubject();
22+
}
23+
_realSubject->request();
24+
}
25+
private:
26+
RealSubject* _realSubject;
27+
};
28+
29+
int main() {
30+
31+
Subject* proxy = new Proxy();
32+
proxy->request();
33+
34+
return 0;
35+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Graphic {
5+
public:
6+
virtual void draw() = 0;
7+
};
8+
9+
class Image : public Graphic {
10+
public:
11+
Image() {
12+
// load image frome disk.
13+
}
14+
virtual void draw() {
15+
cout << "Draw real image." << endl;
16+
}
17+
};
18+
19+
class ProxyImage : public Graphic {
20+
public:
21+
virtual void draw() {
22+
if(_image == NULL) {
23+
_image = new Image();
24+
cout << "Real image is assigned." << endl;
25+
}
26+
_image->draw();
27+
}
28+
private:
29+
Image* _image;
30+
};
31+
32+
int main() {
33+
34+
Graphic* proxyImage = new ProxyImage();
35+
proxyImage->draw();
36+
proxyImage->draw();
37+
proxyImage->draw();
38+
return 0;
39+
}

0 commit comments

Comments
 (0)