1. “单一职责”模式
在软件组件的设计中,如果则热划分的不清晰,使用继承得到的结果往往是需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任。
典型模式
Decorator
Bridge
2. 动机(Motivation)
由于某些类型的固有的实现逻辑,使得它们具有两个变化的维度,乃至多个维度的变化。
如何应对这种“多维度的变化”?如何利用面向对象技术来使得类型可以轻松的沿着两个乃至多个方向变化,而不引入额外的复杂度。
3. 代码
#include<string> using namespace std; class Messager { public: virtual void Login(string username, string password) = 0; virtual void SendMessage(string message) = 0; virtual void SendPicture(Image image) = 0; virtual void PlaySound() = 0; virtual void DrawShape() = 0; virtual void WriteText() = 0; virtual void Connect() = 0; virtual ~Messager(){} }; //平台实现 class PCMessageBase :public Messager { public: virtual void PlaySound() { //... } virtual void DrawShape() { //... } virtual void WriteText() { //... } virtual void Connect() { //... } }; //平台实现 class MobileMessageBase :public Messager { public: virtual void PlaySound() { //... } virtual void DrawShape() { //... } virtual void WriteText() { //... } virtual void Connect() { //... } }; //抽象业务 class PCMessageLite :public PCMessageBase { public: virtual void Login(string username, string password) { PCMessageBase::Connect(); //... } virtual void SendMessage(string message) { PCMessageBase::WriteText(); //... } virtual void SendPicture(Image image) { PCMessageBase::DrawShape(); //... } }; class PCMessagePerfect :public PCMessageBase { public: virtual void Login(string username, string password) { PCMessageBase::PlaySound(); //... PCMessageBase::Connect(); //... } virtual void SendMessage(string message) { PCMessageBase::PlaySound(); //... PCMessageBase::WriteText(); //... } virtual void SendPicture(Image image) { PCMessageBase::PlaySound(); //... PCMessageBase::DrawShape(); //... } }; class MobileMessageLite :public MobileMessageBase { public: virtual void Login(string username, string password) { MobileMessageBase::Connect(); //... } virtual void SendMessage(string message) { MobileMessageBase::WriteText(); //... } virtual void SendPicture(Image image) { MobileMessageBase::DrawShape(); //... } }; class MobileMessagePerfect :public MobileMessageBase { public: virtual void Login(string username, string password) { MobileMessageBase::PlaySound(); //... MobileMessageBase::Connect(); //... } virtual void SendMessage(string message) { MobileMessageBase::PlaySound(); //... MobileMessageBase::WriteText(); //... } virtual void SendPicture(Image image) { MobileMessageBase::PlaySound(); //... MobileMessageBase::DrawShape(); //... } }; void Process() { //编译时装配 Messager* m = new MobileMessagePerfect(); }
各类之间的继承关系如下:
使用Bridge设计模式修改之后的代码
#include<string> using namespace std; class MessagerImp { public: virtual void PlaySound() = 0; virtual void DrawShape() = 0; virtual void WriteText() = 0; virtual void Connect() = 0; virtual ~MessagerImp() {} }; class Messager { protected: MessagerImp* messagerImp; Messager(MessagerImp* m) :messagerImp(m) {}; public: virtual void Login(string username, string password) = 0; virtual void SendMessage(string message) = 0; virtual void SendPicture(Image image) = 0; virtual ~Messager() {} }; //平台实现 class PCMessageImp :public MessagerImp { public: virtual void PlaySound() { //... } virtual void DrawShape() { //... } virtual void WriteText() { //... } virtual void Connect() { //... } }; //移动设备实现 class MobileMessageImp :public MessagerImp { public: virtual void PlaySound() { //... } virtual void DrawShape() { //... } virtual void WriteText() { //... } virtual void Connect() { //... } }; //抽象业务 class MessageLite :public Messager { public: MessageLite(MessagerImp* m) :Messager(m) {} virtual void Login(string username, string password) { messagerImp->Connect(); //... } virtual void SendMessage(string message) { messagerImp->WriteText(); //... } virtual void SendPicture(Image image) { messagerImp->DrawShape(); //... } }; class MessagePerfect :public Messager { public: MessagePerfect(MessagerImp* m) :Messager(m) {} virtual void Login(string username, string password) { messagerImp->PlaySound(); //... messagerImp->Connect(); //... } virtual void SendMessage(string message) { messagerImp->PlaySound(); //... messagerImp->WriteText(); //... } virtual void SendPicture(Image image) { messagerImp->PlaySound(); //... messagerImp->DrawShape(); //... } }; void Process() { //运行时装配 MessagerImp* mImp = new PCMessageImp(); Messager* m = new MessagePerfect(mImp); }
各类之间的继承关系如下:
4. 模式定义
将抽象部分(业务功能)与实现部分(平台实现)分离,使它们都可以独立的变化。
5. 结构
6. 要点总结
Bridge模式使用“对象间的组合关系”解耦了抽象和现实之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化。所谓抽象和实现沿着各自的维度的变化,即“子类化”它们。
Bridge模式有时候类似于多继承方案,但是多继承方案往往违背了单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多继承更好的解决方法。
Bridge模式的应用一般在“两个非常强的变化维度”,有时一个类也有多个变化维度,这时可以使用Bridge的扩展模式。