标签:img number 单一职责 现在 namespace 接口规范 include 继承 isp
场景
decorator1.cpp
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 class CryptoBufferedFileStream :public FileStream{ 126 public: 127 virtual char Read(int number){ 128 129 //额外的加密操作... 130 //额外的缓冲操作... 131 FileStream::Read(number);//读文件流 132 } 133 virtual void Seek(int position){ 134 //额外的加密操作... 135 //额外的缓冲操作... 136 FileStream::Seek(position);//定位文件流 137 //额外的加密操作... 138 //额外的缓冲操作... 139 } 140 virtual void Write(byte data){ 141 //额外的加密操作... 142 //额外的缓冲操作... 143 FileStream::Write(data);//写文件流 144 //额外的加密操作... 145 //额外的缓冲操作... 146 } 147 }; 148 149 void Process(){ 150 151 //编译时装配 152 CryptoFileStream *fs1 = new CryptoFileStream(); 153 154 BufferedFileStream *fs2 = new BufferedFileStream(); 155 156 CryptoBufferedFileStream *fs3 =new CryptoBufferedFileStream(); 157 158 }
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 class CryptoStream: public Stream { 57 58 Stream* stream;//未来可变成各种stream,支持变化 59 60 public: 61 CryptoStream(Stream* stm):stream(stm){ 62 63 } 64 65 virtual char Read(int number){ 66 67 //额外的加密操作... 68 stream->Read(number);//读文件流 69 } 70 virtual void Seek(int position){ 71 //额外的加密操作... 72 stream::Seek(position);//定位文件流 73 //额外的加密操作... 74 } 75 virtual void Write(byte data){ 76 //额外的加密操作... 77 stream::Write(data);//写文件流 78 //额外的加密操作... 79 } 80 }; 81 82 class BufferedStream : public Stream{ 83 84 Stream* stream;//... 85 86 public: 87 BufferedStream(Stream* stm):stream(stm){ 88 89 } 90 //... 91 }; 92 93 void Process(){ 94 //运行时装配 95 FileStream* s1=new FileStream(); 96 97 //加密 98 CryptoStream* s2=new CryptoStream(s1); 99 100 //缓存 101 BufferedStream* s3=new BufferedStream(s1); 102 103 //既加密又缓存 104 BufferedStream* s4=new BufferedStream(s2); 105 }
decorator3.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 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 public: 70 CryptoStream(Stream* stm):DecoratorStream(stm){ 71 72 } 73 74 75 virtual char Read(int number){ 76 77 //额外的加密操作... 78 stream->Read(number);//读文件流 79 } 80 virtual void Seek(int position){ 81 //额外的加密操作... 82 stream::Seek(position);//定位文件流 83 //额外的加密操作... 84 } 85 virtual void Write(byte data){ 86 //额外的加密操作... 87 stream::Write(data);//写文件流 88 //额外的加密操作... 89 } 90 }; 91 92 class BufferedStream : public DecoratorStream{ 93 94 Stream* stream;//... 95 96 public: 97 BufferedStream(Stream* stm):DecoratorStream(stm){ 98 99 } 100 //... 101 }; 102 103 void Process(){ 104 105 //运行时装配 106 FileStream* s1=new FileStream(); 107 108 CryptoStream* s2=new CryptoStream(s1); 109 110 BufferedStream* s3=new BufferedStream(s1); 111 112 BufferedStream* s4=new BufferedStream(s2); 113 114 }
总结
示例
1 #include <string> 2 #include <iostream> 3 using namespace std; 4 5 class Person{ 6 private: 7 string m_strName; 8 public: 9 Person(string strName){ 10 m_strName=strName; 11 } 12 Person(){} 13 virtual void Show(){ 14 cout<<"装扮的是"<<m_strName<<endl; 15 } 16 }; 17 18 class Finery:public Person{ 19 protected: 20 Person* m_component; 21 public: 22 void Decorate(Person* component){ 23 m_component = component; 24 } 25 virtual void Show(){ 26 m_component->Show(); 27 } 28 }; 29 30 class TShirts:public Finery{ 31 public: 32 virtual void Show(){ 33 cout<<"T Shirts"<<endl; 34 m_component->Show(); 35 } 36 }; 37 38 class BigTrouser:public Finery{ 39 public: 40 virtual void Show(){ 41 cout<<"Big Trouser"<<endl; 42 m_component->Show(); 43 } 44 }; 45 46 int main(){ 47 Person *p = new Person("小李"); 48 BigTrouser *bt = new BigTrouser(); 49 TShirts *ts = new TShirts(); 50 bt->Decorate(p); 51 ts->Decorate(bt); 52 ts->Show(); 53 return 0; 54 }
标签:img number 单一职责 现在 namespace 接口规范 include 继承 isp
原文地址:https://www.cnblogs.com/cxc1357/p/12293267.html