Kyle Chen's Blog

Action speaks louder than Words

0%

代理模式C++实现

ref : 代理模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<iostream>
using namespace std;

class VideoSite{
public:
virtual void watchFreemovie() = 0;
virtual void watchVipmovie() = 0;
virtual void watchSVipmovie() = 0;
};

class RealSubject:public VideoSite{
public:
void watchFreemovie(){
cout<<"You are watching Free Movie."<<endl;
}
void watchVipmovie(){
cout<<"You are watching Vip Movie."<<endl;
}
void watchSVipmovie(){
cout<<"You are watching SVip Movie."<<endl;
}
};

class FreeUesrProxy:public VideoSite{
private:
RealSubject* realSubject;
public:
FreeUesrProxy(){
realSubject = new RealSubject;
}
~FreeUesrProxy(){
delete realSubject;
realSubject = NULL;
}
void watchFreemovie(){
realSubject->watchFreemovie();
}
void watchVipmovie(){
cout<<"You cannot watch Vip Movie."<<endl;
}
void watchSVipmovie(){
cout<<"You cannot watch SVip Movie."<<endl;
}

};

class VipUserProxy:public VideoSite{
private:
RealSubject* realSubject;
public:
VipUserProxy(){
realSubject = new RealSubject;
}
~VipUserProxy(){
delete realSubject;
realSubject = NULL;
}
void watchFreemovie(){
realSubject->watchFreemovie();
}
void watchVipmovie(){
realSubject->watchVipmovie();
}
void watchSVipmovie(){
cout<<"You cannot watch SVip Movie."<<endl;
}
};

class SVipUserProxy:public VideoSite{
private:
RealSubject* realSubject;
public:

SVipUserProxy(){
realSubject = new RealSubject;
}
~SVipUserProxy(){
delete realSubject;
realSubject = NULL;
}
void watchFreemovie(){
realSubject->watchFreemovie();
}
void watchVipmovie(){
realSubject->watchVipmovie();
}
void watchSVipmovie(){
realSubject->watchSVipmovie();
}

};


void watchMovie(VideoSite* videoSite){
videoSite->watchFreemovie();
videoSite->watchVipmovie();
videoSite->watchSVipmovie();
}



int main(){
VideoSite* videoSite = new VipUserProxy();
watchMovie(videoSite);

}