-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoratorMethod.h
130 lines (103 loc) · 2.19 KB
/
decoratorMethod.h
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <iostream>
using namespace std;
//装饰模式
//业务操作
class Stream {
public:
virtual void Read(int number) = 0;
virtual void Seek(int position) = 0;
virtual void Write(char data) = 0;
virtual ~Stream() {}
};
//主体类
class FileStream : public Stream {
public:
virtual void Read(int number) {
//读文件流
cout << "FileStream::Read" << endl;
}
virtual void Seek(int position) {
//定位文件流
cout << "FileStream::Seek" << endl;
}
virtual void Write(char data) {
//写文件流
cout << "FileStream::Write" << endl;
}
};
class NetworkStream :public Stream {
public:
virtual void Read(int number) {
//读文件流
cout << "NetworkStream::Read" << endl;
}
virtual void Seek(int position) {
//定位文件流
cout << "NetworkStream::Seek" << endl;
}
virtual void Write(char data) {
//写文件流
cout << "NetworkStream::Write" << endl;
}
};
class MemoryStream :public Stream {
public:
virtual void Read(int number) {
//读文件流
cout << "MemoryStream::Read" << endl;
}
virtual void Seek(int position) {
//定位文件流
cout << "MemoryStream::Seek" << endl;
}
virtual void Write(char data) {
//写文件流
cout << "MemoryStream::Write" << endl;
}
};
//扩展操作
class DecoratorStream : public Stream {
protected:
Stream* stream;//...
DecoratorStream(Stream* stm) : stream(stm) {
}
};
class CryptoStream : public DecoratorStream {
public:
CryptoStream(Stream* stm) : DecoratorStream(stm) {
}
virtual void Read(int number) {
//额外的加密操作...
stream->Read(number);//读文件流
}
virtual void Seek(int position) {
//额外的加密操作...
stream->Seek(position);//定位文件流
//额外的加密操作...
}
virtual void Write(char data) {
//额外的加密操作...
stream->Write(data);//写文件流
//额外的加密操作...
}
};
class BufferedStream : public DecoratorStream {
public:
BufferedStream(Stream* stm) :DecoratorStream(stm) {
}
virtual void Read(int number) {
//额外的加密操作...
stream->Read(number);//读文件流
}
virtual void Seek(int position) {
//额外的加密操作...
stream->Seek(position);//定位文件流
//额外的加密操作...
}
virtual void Write(char data) {
//额外的加密操作...
stream->Write(data);//写文件流
//额外的加密操作...
}
};