动机(Motivation)
-
在某些情况下我们可能会“过度地使用继承来扩展对象的功能”,由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性; 并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀。
-
如何使“对象功能的扩展”能够根据需要来动态地实现?同时避免“扩展功能的增多”带来的子类膨胀问题?从而使得任何“功能扩展变化”所导致的影响将为最低?
模式定义
动态(组合)地给一个对象增加一些额外的职责。就增加功能而言,Decorator模式比生成子类(继承)更为灵活(消除重复代码 & 减少子类个数)。 ——《设计模式》GoF
顾名思义:装饰者就是对原有的功能进行新的装饰,其是通过一种动态的方式来扩展一个对象的功能,在运行时选择不同的装饰器,从而实现不同的行为。其比继承灵活一些,但是出bug不容易查找定位。
要点总结
-
通过采用组合而非继承的手法, Decorator模式实现了在运行时动态扩展对象功能的能力,而且可以根据需要扩展多个功能。 避免了使用继承带来的“灵活性差”和“多子类衍生问题”。
-
Decorator类在接口上表现为is-a Component的继承关系,即Decorator类继承了Component类所具有的接口。 但在实现上又表现为has-a Component的组合关系,即Decorator类又使用了另外一个Component类。
-
Decorator模式的目的并非解决“多子类衍生的多继承”问题,Decorator模式应用的要点在于解决“主体类在多个方向上的扩展功能”——是为“装饰”的含义。
-
Decorator模式中既有继承也有组合,其来解决过度继承问题
非装饰者模式
#include <iostream>
#include <string>
using namespace std;
//业务操作
class Stream
{
public:
virtual void Read() = 0;
virtual void Write() = 0;
virtual ~Stream() {}
};
//主体类
class FileStream : public Stream
{
public:
virtual void Read()
{
//读文件流
cout << "FileStream Read" << endl;
}
virtual void Write()
{
//写文件流
cout << "NetworkStream Write" << endl;
}
};
class NetworkStream :public Stream
{
public:
virtual void Read()
{
//读网络流
cout << "NetworkStream Read" << endl;
}
virtual void Write()
{
//写网络流
cout << "NetworkStream Write" << endl;
}
};
class MemoryStream :public Stream
{
public:
virtual void Read()
{
//读内存流
cout << "MemoryStream Read" << endl;
}
virtual void Write()
{
//写内存流
cout << "MemoryStream Write" << endl;
}
};
//扩展操作
class CryptoFileStream :public FileStream
{
public:
virtual void Read()
{
//额外的加密操作...
cout << "CryptoFileStream Read" << endl;
FileStream::Read();//读文件流
}
virtual void Write( )
{
//额外的加密操作...
cout << "CryptoFileStream Write" << endl;
FileStream::Write();//写文件流
//额外的加密操作...
}
};
class CryptoNetworkStream : public NetworkStream
{
public:
virtual void Read()
{
//额外的加密操作...
cout << "CryptoNetworkStream Read" << endl;
NetworkStream::Read();//读网络流
}
virtual void Write()
{
//额外的加密操作...
cout << "CryptoNetworkStream Write" << endl;
NetworkStream::Write();//写网络流
}
};
class CryptoMemoryStream : public MemoryStream
{
public:
virtual void Read()
{
//额外的加密操作...
cout << "CryptoMemoryStream Read" << endl;
MemoryStream::Read();//读内存流
}
virtual void Write()
{
//额外的加密操作...
cout << "CryptoMemoryStream Write" << endl;
MemoryStream::Write();//写内存流
//额外的加密操作...
}
};
class BufferedFileStream : public FileStream
{
public:
virtual void Read()
{
//额外的加密操作...
cout << "BufferedFileStream Read" << endl;
FileStream::Read();//读内存流
}
virtual void Write()
{
//额外的加密操作...
cout << "BufferedFileStream Write" << endl;
FileStream::Write();//写内存流
//额外的加密操作...
}
};
class BufferedNetworkStream : public NetworkStream
{
public:
virtual void Read()
{
//额外的加密操作...
cout << "BufferedFileStream Read" << endl;
NetworkStream::Read();//读内存流
}
virtual void Write()
{
//额外的加密操作...
cout << "BufferedFileStream Write" << endl;
NetworkStream::Write();//写内存流
//额外的加密操作...
}
};
class BufferedMemoryStream : public MemoryStream
{
public:
virtual void Read()
{
//额外的加密操作...
cout << "BufferedFileStream Read" << endl;
MemoryStream::Read();//读内存流
}
virtual void Write()
{
//额外的加密操作...
cout << "BufferedFileStream Write" << endl;
MemoryStream::Write();//写内存流
//额外的加密操作...
}
};
class CryptoBufferedFileStream :public FileStream
{
public:
virtual void Read()
{
//额外的加密操作...
cout << "CryptoFileStream Read" << endl;
//额外的缓冲操作...
cout << "BufferedFileStream Read" << endl;
FileStream::Read();//读文件流
}
virtual void Write()
{
//额外的加密操作...
//额外的缓冲操作...
FileStream::Write();//写文件流
//额外的加密操作...
//额外的缓冲操作...
}
};
int main()
{
//编译时装配
CryptoFileStream *fs1 = new CryptoFileStream();
fs1->Read();
BufferedFileStream *fs2 = new BufferedFileStream();
fs2->Read();
CryptoBufferedFileStream *fs3 = new CryptoBufferedFileStream();
fs3->Read();
}
装饰者模式
#include <iostream>
#include <string>
using namespace std;
//业务操作
class Stream
{
public:
virtual void Read() = 0;
virtual void Write() = 0;
virtual ~Stream() {}
};
//主体类
class FileStream : public Stream
{
public:
virtual void Read()
{
//读文件流
cout << "FileStream Read" << endl;
}
virtual void Write()
{
//写文件流
cout << "FileStream Write" << endl;
}
};
class NetworkStream :public Stream
{
public:
virtual void Read()
{
//读网络流
cout << "NetworkStream Read" << endl;
}
virtual void Write()
{
//写网络流
cout << "NetworkStream Write" << endl;
}
};
class MemoryStream :public Stream
{
public:
virtual void Read()
{
//读内存流
cout << "MemoryStream Read" << endl;
}
virtual void Write()
{
//写内存流
cout << "MemoryStream Write" << endl;
}
};
//装饰类
class DecoratorStream :public Stream
{
protected:
Stream *m_stream;
public:
DecoratorStream(Stream *st)
{
m_stream = st;
}
};
//扩展操作
// 这块为了简便没有区分CryptoFileStream、CryptoNetworkStream和CryptoMemoryStream的输出内容不同。其主体思想就是CryptoFileStream等三个不同对象调用不同的实现方式。
class CryptoStream :public DecoratorStream
{
public:
CryptoStream(Stream *st) :DecoratorStream(st){}
virtual void Read()
{
//额外的加密操作...
cout << "CryptoStream Read" << endl;
m_stream->Read();//读文件流
}
virtual void Write( )
{
//额外的加密操作...
cout << "CryptoStream Write" << endl;
m_stream->Write();//写文件流
//额外的加密操作...
}
};
class BufferedStream : public DecoratorStream
{
public:
BufferedStream(Stream *st) :DecoratorStream(st) {}
virtual void Read()
{
//额外的加密操作...
cout << "BufferedStream Read" << endl;
m_stream->Read();//读内存流
}
virtual void Write()
{
//额外的加密操作...
cout << "BufferedStream Write" << endl;
m_stream->Write();//写内存流
//额外的加密操作...
}
};
class CryptoBufferedFileStream :public DecoratorStream
{
public:
CryptoBufferedFileStream(Stream *st) :DecoratorStream(st) {}
virtual void Read()
{
//额外的加密操作...
cout << "CryptoStream Read" << endl;
//额外的缓冲操作...
cout << "BufferedStream Read" << endl;
m_stream->Read();//读文件流
}
virtual void Write()
{
//额外的加密操作...
//额外的缓冲操作...
m_stream->Write();//写文件流
//额外的加密操作...
//额外的缓冲操作...
}
};
int main()
{
//编译时装配
FileStream *fs = new FileStream();
CryptoStream *fs1 = new CryptoStream(fs);
fs1->Read();
BufferedStream *fs2 = new BufferedStream(fs);
fs2->Read();
NetworkStream *nt = new NetworkStream();
CryptoBufferedFileStream *fs3 = new CryptoBufferedFileStream(nt);
fs3->Read();
}