1. 不好的代码(冗杂)
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 CryptoFileStream :public FileStream{
public:virtual char Read(int number){FileStream::Read(number);}virtual void Seek(int position){FileStream::Seek(position);}virtual void Write(byte data){FileStream::Write(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(byte data){NetworkStream::Write(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(byte data){MemoryStream::Write(data);}
};class BufferedFileStream : public FileStream{
};class BufferedNetworkStream : public NetworkStream{
};class BufferedMemoryStream : public MemoryStream{
}class CryptoBufferedFileStream :public FileStream{
public:virtual char Read(int number){FileStream::Read(number);}virtual void Seek(int position){FileStream::Seek(position);}virtual void Write(byte data){FileStream::Write(data);}
};void Process(){CryptoFileStream *fs1 = new CryptoFileStream();BufferedFileStream *fs2 = new BufferedFileStream();CryptoBufferedFileStream *fs3 =new CryptoBufferedFileStream();}
2. 好的代码
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* stream;public:CryptoStream(Stream* stm):stream(stm){}virtual char Read(int number){stream->Read(number);}virtual void Seek(int position){stream->Seek(position);}virtual void Write(byte data){stream->Write(data);}
};class BufferedStream : public Stream{Stream* stream;public:BufferedStream(Stream* stm):stream(stm){}
};void Process(){FileStream* s1=new FileStream();CryptoStream* s2=new CryptoStream(s1);BufferedStream* s3=new BufferedStream(s1);BufferedStream* s4=new BufferedStream(s2);
}
3. 继续改进
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){}};DecoratorStream: public Stream{
protected:Stream* stream;DecoratorStream(Stream * stm):stream(stm){}};class CryptoStream: public DecoratorStream {public:CryptoStream(Stream* stm):DecoratorStream(stm){}virtual char Read(int number){stream->Read(number);}virtual void Seek(int position){stream->Seek(position);}virtual void Write(byte data){stream->Write(data);}
};class BufferedStream : public DecoratorStream{Stream* stream;public:BufferedStream(Stream* stm):DecoratorStream(stm){}
};void Process(){FileStream* s1=new FileStream();CryptoStream* s2=new CryptoStream(s1);BufferedStream* s3=new BufferedStream(s1);BufferedStream* s4=new BufferedStream(s2);
}