-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator.cpp
159 lines (141 loc) · 2.89 KB
/
decorator.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//装饰器模式是用来给对象增加某些特性或者对被装饰对象进行某些修改
//典型应用场景:Java的IO流
#include <iostream>
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)
{
std::cout << "FileStream Read" << std::endl;
}
virtual void Seek(int position)
{
std::cout << "FileStream Seek" << std::endl;
}
virtual void Write(char data)
{
std::cout << "FileStream Write" << std::endl;
}
};
class NetworkStream :public Stream
{
public:
virtual void Read(int number)
{
std::cout << "NetworkStream Read" << std::endl;
}
virtual void Seek(int position)
{
std::cout << "NetworkStream Seek" << std::endl;
}
virtual void Write(char data)
{
std::cout << "NetworkStream Write" << std::endl;
}
};
class MemoryStream :public Stream
{
public:
virtual void Read(int number)
{
std::cout << "MemoryStream Read" << std::endl;
}
virtual void Seek(int position)
{
std::cout << "MemoryStream Seek" << std::endl;
}
virtual void Write(char data)
{
std::cout << "MemoryStream wRITE" << std::endl;
}
};
//扩展操作
class DecoratorStream :public Stream
{
public:
Stream* stream;
DecoratorStream(Stream* stm) :stream(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 CryptoSteram :public DecoratorStream
{
public:
CryptoSteram(Stream* stm):DecoratorStream(stm)
{
}
virtual void Read(int number)
{
std::cout << "CryptoSteram加密操作" << std::endl;
stream->Read(number);
}
virtual void Seek(int position)
{
std::cout << "CryptoSteram加密操作" << std::endl;
stream->Seek(position);
}
virtual void Write(char data)
{
std::cout << "CryptoSteram加密操作" << std::endl;
stream->Write(data);
}
};
class BufferedStream :public DecoratorStream
{
public:
BufferedStream(Stream* stm) :DecoratorStream(stm)
{
}
virtual void Read(int number)
{
std::cout << "BufferedStream加密操作" << std::endl;
stream->Read(number);
}
virtual void Seek(int position)
{
std::cout << "BufferedStream加密操作" << std::endl;
stream->Seek(position);
}
virtual void Write(char data)
{
std::cout << "BufferedStream加密操作" << std::endl;
stream->Write(data);
}
};
//int main()
//{
// FileStream* s1 = new FileStream();
// NetworkStream* s2 = new NetworkStream();
// MemoryStream* s3 = new MemoryStream();
//
// DecoratorStream* cryp = new CryptoSteram(s1);
// DecoratorStream* buff = new BufferedStream(new CryptoSteram(s2));
//
// s1->Read(3);
// s2->Read(4);
// s3->Read(5);
// cryp->Read(6);
// buff->Read(7);
//
//}