装饰模式
在软件组件的设计中,如果责任划分的不清楚,使用继承得到的结果往往 是随着需求的变化,
子类极具膨胀,同时充斥这重复代码
由继承为类型引入的静态特质,缺乏灵活性,随着子类的增多,各种组合导致子类膨胀
如下的对流的操作,扩展出文件流,网络流,内存流。以及扩展的加密及缓存导致代码膨胀
decorator1.cpp (1+n+n*m!/2)
1 //业务操作 2 class Stream{ 3 public: 4 virtual char Read(int number)=0; 5 virtual void Seek(int position)=0; 6 virtual void Write(char data)=0; 7 8 virtual ~Stream(){} 9 }; 10 11 //主体类 12 class FileStream: public Stream{ 13 public: 14 virtual char Read(int number){ 15 //读文件流 16 } 17 virtual void Seek(int position){ 18 //定位文件流 19 } 20 virtual void Write(char data){ 21 //写文件流 22 } 23 24 }; 25 26 class NetworkStream :public Stream{ 27 public: 28 virtual char Read(int number){ 29 //读网络流 30 } 31 virtual void Seek(int position){ 32 //定位网络流 33 } 34 virtual void Write(char data){ 35 //写网络流 36 } 37 38 }; 39 40 class MemoryStream :public Stream{ 41 public: 42 virtual char Read(int number){ 43 //读内存流 44 } 45 virtual void Seek(int position){ 46 //定位内存流 47 } 48 virtual void Write(char data){ 49 //写内存流 50 } 51 52 }; 53 54 //扩展操作 55 class CryptoFileStream :public FileStream{ 56 public: 57 virtual char Read(int number){ 58 59 //额外的加密操作... 60 FileStream::Read(number);//读文件流 61 62 } 63 virtual void Seek(int position){ 64 //额外的加密操作... 65 FileStream::Seek(position);//定位文件流 66 //额外的加密操作... 67 } 68 virtual void Write(byte data){ 69 //额外的加密操作... 70 FileStream::Write(data);//写文件流 71 //额外的加密操作... 72 } 73 }; 74 75 class CryptoNetworkStream : :public NetworkStream{ 76 public: 77 virtual char Read(int number){ 78 79 //额外的加密操作... 80 NetworkStream::Read(number);//读网络流 81 } 82 virtual void Seek(int position){ 83 //额外的加密操作... 84 NetworkStream::Seek(position);//定位网络流 85 //额外的加密操作... 86 } 87 virtual void Write(byte data){ 88 //额外的加密操作... 89 NetworkStream::Write(data);//写网络流 90 //额外的加密操作... 91 } 92 }; 93 94 class CryptoMemoryStream : public MemoryStream{ 95 public: 96 virtual char Read(int number){ 97 98 //额外的加密操作... 99 MemoryStream::Read(number);//读内存流 100 } 101 virtual void Seek(int position){ 102 //额外的加密操作... 103 MemoryStream::Seek(position);//定位内存流 104 //额外的加密操作... 105 } 106 virtual void Write(byte data){ 107 //额外的加密操作... 108 MemoryStream::Write(data);//写内存流 109 //额外的加密操作... 110 } 111 }; 112 113 class BufferedFileStream : public FileStream{ 114 //... 115 }; 116 117 class BufferedNetworkStream : public NetworkStream{ 118 //... 119 }; 120 121 class BufferedMemoryStream : public MemoryStream{ 122 //... 123 } 124 125 126 127 128 class CryptoBufferedFileStream :public FileStream{ 129 public: 130 virtual char Read(int number){ 131 132 //额外的加密操作... 133 //额外的缓冲操作... 134 FileStream::Read(number);//读文件流 135 } 136 virtual void Seek(int position){ 137 //额外的加密操作... 138 //额外的缓冲操作... 139 FileStream::Seek(position);//定位文件流 140 //额外的加密操作... 141 //额外的缓冲操作... 142 } 143 virtual void Write(byte data){ 144 //额外的加密操作... 145 //额外的缓冲操作... 146 FileStream::Write(data);//写文件流 147 //额外的加密操作... 148 //额外的缓冲操作... 149 } 150 }; 151 152 153 154 void Process(){ 155 156 //编译时装配 157 CryptoFileStream *fs1 = new CryptoFileStream(); 158 159 BufferedFileStream *fs2 = new BufferedFileStream(); 160 161 CryptoBufferedFileStream *fs3 =new CryptoBufferedFileStream(); 162 163 }
将扩展类中的基类部分声明为基类指针(字段),在编译时复用,通过多态的方式让其复用,
在运行时变化,
decorator2.cpp
1 //业务操作 2 class Stream{ 3 4 public: 5 virtual char Read(int number)=0; 6 virtual void Seek(int position)=0; 7 virtual void Write(char data)=0; 8 9 virtual ~Stream(){} 10 }; 11 12 //主体类 13 class FileStream: public Stream{ 14 public: 15 virtual char Read(int number){ 16 //读文件流 17 } 18 virtual void Seek(int position){ 19 //定位文件流 20 } 21 virtual void Write(char data){ 22 //写文件流 23 } 24 25 }; 26 27 class NetworkStream :public Stream{ 28 public: 29 virtual char Read(int number){ 30 //读网络流 31 } 32 virtual void Seek(int position){ 33 //定位网络流 34 } 35 virtual void Write(char data){ 36 //写网络流 37 } 38 39 }; 40 41 class MemoryStream :public Stream{ 42 public: 43 virtual char Read(int number){ 44 //读内存流 45 } 46 virtual void Seek(int position){ 47 //定位内存流 48 } 49 virtual void Write(char data){ 50 //写内存流 51 } 52 53 }; 54 55 //扩展操作 56 57 58 class CryptoStream: public Stream { 59 60 Stream* stream;//... 61 62 public: 63 CryptoStream(Stream* stm):stream(stm){ 64 65 } 66 67 68 virtual char Read(int number){ 69 70 //额外的加密操作... 71 stream->Read(number);//读文件流 72 } 73 virtual void Seek(int position){ 74 //额外的加密操作... 75 stream::Seek(position);//定位文件流 76 //额外的加密操作... 77 } 78 virtual void Write(byte data){ 79 //额外的加密操作... 80 stream::Write(data);//写文件流 81 //额外的加密操作... 82 } 83 }; 84 85 86 87 class BufferedStream : public Stream{ 88 89 Stream* stream;//... 90 91 public: 92 BufferedStream(Stream* stm):stream(stm){ 93 94 } 95 //... 96 }; 97 98 99 100 101 102 void Process(){ 103 104 //运行时装配 105 FileStream* s1=new FileStream(); 106 CryptoStream* s2=new CryptoStream(s1); 107 108 BufferedStream* s3=new BufferedStream(s1); 109 110 BufferedStream* s4=new BufferedStream(s2); 111 112 113 114 }
decorator3.cpp (1+n+1+m)
1 //业务操作 2 class Stream{ 3 4 public: 5 virtual char Read(int number)=0; 6 virtual void Seek(int position)=0; 7 virtual void Write(char data)=0; 8 9 virtual ~Stream(){} 10 }; 11 12 //主体类 13 class FileStream: public Stream{ 14 public: 15 virtual char Read(int number){ 16 //读文件流 17 } 18 virtual void Seek(int position){ 19 //定位文件流 20 } 21 virtual void Write(char data){ 22 //写文件流 23 } 24 25 }; 26 27 class NetworkStream :public Stream{ 28 public: 29 virtual char Read(int number){ 30 //读网络流 31 } 32 virtual void Seek(int position){ 33 //定位网络流 34 } 35 virtual void Write(char data){ 36 //写网络流 37 } 38 39 }; 40 41 class MemoryStream :public Stream{ 42 public: 43 virtual char Read(int number){ 44 //读内存流 45 } 46 virtual void Seek(int position){ 47 //定位内存流 48 } 49 virtual void Write(char data){ 50 //写内存流 51 } 52 53 }; 54 55 //扩展操作 56 57 DecoratorStream: public Stream{ 58 protected: 59 Stream* stream;//... 60 61 DecoratorStream(Stream * stm):stream(stm){ 62 63 } 64 65 }; 66 67 class CryptoStream: public DecoratorStream { 68 69 70 public: 71 CryptoStream(Stream* stm):DecoratorStream(stm){ 72 73 } 74 75 76 virtual char Read(int number){ 77 78 //额外的加密操作... 79 stream->Read(number);//读文件流 80 } 81 virtual void Seek(int position){ 82 //额外的加密操作... 83 stream::Seek(position);//定位文件流 84 //额外的加密操作... 85 } 86 virtual void Write(byte data){ 87 //额外的加密操作... 88 stream::Write(data);//写文件流 89 //额外的加密操作... 90 } 91 }; 92 93 94 95 class BufferedStream : public DecoratorStream{ 96 97 Stream* stream;//... 98 99 public: 100 BufferedStream(Stream* stm):DecoratorStream(stm){ 101 102 } 103 //... 104 }; 105 106 107 108 109 void Process(){ 110 111 //运行时装配 112 FileStream* s1=new FileStream(); 113 114 CryptoStream* s2=new CryptoStream(s1); 115 116 BufferedStream* s3=new BufferedStream(s1); 117 118 BufferedStream* s4=new BufferedStream(s2); 119 120 121 122 }