File tree 2 files changed +74
-0
lines changed
2 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments