装饰者模式
例子
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
| class Stream{ public: virtual char Read(int number)=0; virtual void Seek(int position)=0; virtual void Write(char data)=0;
virtual ~Stream(); };
class FileStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class NetworkStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class MemoryStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class CryptoNetworkStream:public NetworkStream{ public: virtual char Read(int number){ NetworkStream::read(number); } virtual void Seek(int position){ NetworkStream::Seek(position); } virtual void Write(char data){ NetworkStream::Seek(data); } }
class CryptoFileStream:public FileStream{ public: virtual char Read(int number){ FileStream::read(number); } virtual void Seek(int position){ FileStream::Seek(position); } virtual void Write(char data){ FileStream::Seek(data); } }
class CryptoMemoryStream:public MemoryStream{ public: virtual char Read(int number){ MemoryStream::read(number); } virtual void Seek(int position){ MemoryStream::Seek(position); } virtual void Write(char data){ MemoryStream::Seek(data); } }
class BufferedMemoryStream : public MemoryStream{ } class BufferedFileStream : public FileStream{ } class BufferedNetworkStream : public NetworkStream{ }
class CryptoBufferedMemoryStream : public CryptoMemoryStream{ } class CryptoBufferedFileStream : public CryptoFileStream{ } class CryptoBufferedNetworkStream : public CryptoNetworkStream{ }
|
子类图示
问题
改进
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
| class Stream{ public: virtual char Read(int number)=0; virtual void Seek(int position)=0; virtual void Write(char data)=0;
virtual ~Stream(); };
class FileStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class NetworkStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class MemoryStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class CryptoNetworkStream{ Stream* stream; public: virtual char Read(int number){ stream->read(number);
} virtual void Seek(int position){ stream->Seek(position); } virtual void Write(char data){ stream->Seek(position); } }
class CryptoFileStream{ Stream* stream; public: virtual char Read(int number){ stream->read(number); } virtual void Seek(int position){ stream->Seek(position); } virtual void Write(char data){ stream->Seek(position); } }
class CryptoMemoryStream{ Stream* stream; public: virtual char Read(int number){ stream->read(number); } virtual void Seek(int position){ stream->Seek(position); } virtual void Write(char data){ stream->Write(position); } }
class BufferedMemoryStream : public MemoryStream{ } class BufferedFileStream : public FileStream{ } class BufferedNetworkStream : public NetworkStream{ }
class CryptoBufferedMemoryStream : public CryptoMemoryStream{ } class CryptoBufferedFileStream : public CryptoFileStream{ } class CryptoBufferedNetworkStream : public CryptoNetworkStream{ }
|
整理
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
| class Stream{ public: virtual char Read(int number)=0; virtual void Seek(int position)=0; virtual void Write(char data)=0;
virtual ~Stream(); };
class FileStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class NetworkStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class MemoryStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class CryptoStream:public Stream{
Stream* m_stream; public: virtual char Read(int number){ stream->read(number); } virtual void Seek(int position){ stream->Seek(position); } virtual void Write(char data){ stream->Seek(position); }
CryptoStream(Stream* stream):m_stream(stream){ }
}
class BufferedStream : public Stream{ Stream* m_stream; public: virtual char Read(int number){ stream->read(number); } virtual void Seek(int position){ stream->Seek(position); } virtual void Write(char data){ stream->Seek(position); }
BufferedStream(Stream* stream):m_stream(stream){ } }
CryptoStream* fileCryptonStream = new CryptoStream(new FileStream()); CryptoStream* networkCryptonStream = new CryptoStream(new NetworkStream()); CryptoStream* memoryCryptonStream = new CryptoStream(new MemoryStream());
BufferedStream* cryptoBufferFileStream = new BufferedStream(new CryptoStream(new FileStream()))
|
进一步改进
- 因为具有公共父类的子类中都含有某一对象
- 顾应该将其抽象到父类中
- 但 FileStream 并不需要 m_stream 因为他是自己来实现功能那个的。顾新建一个子类 DecoratorStream
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
| class Stream{ public: virtual char Read(int number)=0; virtual void Seek(int position)=0; virtual void Write(char data)=0;
virtual ~Stream(); };
class FileStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class NetworkStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class MemoryStream:public Stream{ public: virtual char Read(int number){
} virtual void Seek(int position){
} virtual void Write(char data){
} }
class DecoratorStream:public Stream{ protected: Stream* m_stream;
public: DecoratorStream(Stream* stream):m_stream(stream){
} } class CryptoStream:public DecoratorStream{ public: virtual char Read(int number){ stream->read(number); } virtual void Seek(int position){ stream->Seek(position); } virtual void Write(char data){ stream->Seek(position); }
CryptoStream(DecoratorStream* stream):m_stream(stream){ }
}
class BufferedStream : public DecoratorStream{ public: virtual char Read(int number){ stream->read(number); } virtual void Seek(int position){ stream->Seek(position); } virtual void Write(char data){ stream->Seek(position); }
BufferedStream(DecoratorStream* stream):m_stream(stream){ } }
|
改进后图示
UML图
模式定义
动态(组合)的给一个对象增加一些额外的职责。就增加功能而言
要点总结
- 通过采用组合而非继承的手法,Decorator 模式实现了在运行时动态扩展对象的能力,而且可以根据需要扩展多个功能。避免了使用继承带来的“灵活性差”和“多子类延伸问题”
- Decorator 类在接口表现为 “is-a” Component的继承关系,但在实现上又表现为 “has-a” Component 的组合关系。及Decorator 类又实用了另外一个Component类
- Decorator 模式的目的并非解决“多子类衍生的多继承“问题,Decorator 模式应用的要点在于解决”主体类在多个方向上的扩展功能“—”装饰“的含义