- 使用对象间的组合关系解耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化
由于某些类型固有的实现逻辑,使得它们具有两个变化的维度,乃至多个维度的变化。
将抽象部分(业务功能)与实现部分(平台实现)分离,使它们都可以独立的变化。
class Messager{
public:
virtual void Login(string username, stirng passwaed) = 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(){}
};
// 平台实现 n
class PCMessagerBase : public Messager{
public:
virtual void PlaySound(){
//...
}
virtual void DrawShape(){
//...
}
virtual void WriteText(){
//...
}
virtual void Connect(){
//...
}
};
class MobileMessagerBase : public Messager{
public:
virtual void PlaySound(){
//...
}
virtual void DrawShape(){
//...
}
virtual void WriteText(){
//...
}
virtual void Connect(){
//...
}
};
//业务抽象 m
class PCMessagerLite : public PCMessagerBase{
public:
virtual void Login(string username, stirng passwaed){
PCMessagerBase::Connect();
}
virtual void SendMessage(string message){
PCMessagerBase::WriteText();
}
virtual void SendPicture(Image image){
PCMessagerBase::DrawShape();
}
};
class PCMessagerPerfect : public PCMessagerBase{
public:
virtual void Login(string username, stirng passwaed){
PCMessagerBase::PlaySound();
PCMessagerBase::Connect();
}
virtual void SendMessage(string message){
PCMessagerBase::PlaySound();
PCMessagerBase::WriteText();
}
virtual void SendPicture(Image image){
PCMessagerBase::PlaySound();
PCMessagerBase::DrawShape();
}
};
class MobileMessagerLite : public MobileMessagerBase{
public:
virtual void Login(string username, stirng passwaed){
MobileMessagerBase::Connect();
}
virtual void SendMessage(string message){
MobileMessagerBase::WriteText();
}
virtual void SendPicture(Image image){
MobileMessagerBase::DrawShape();
}
};
class MobileMessagerPerfect : public MobileMessagerBase{
public:
virtual void Login(string username, stirng passwaed){
MobileMessagerBase::PlaySound();
MobileMessagerBase::Connect();
}
virtual void SendMessage(string message){
MobileMessagerBase::PlaySound();
MobileMessagerBase::WriteText();
}
virtual void SendPicture(Image image){
MobileMessagerBase::PlaySound();
MobileMessagerBase::DrawShape();
}
};
void Process(){
// 编译时装配
Message* m = new MobileMessagerPerfect();
}
类的规模:1 + n + m*n
class Messager{
protected:
MessagerImp* messagerImp;
public:
Messager(MessagerImp* Imp) : messagerImp(Imp){}
virtual void Login(string username, stirng passwaed) = 0;
virtual void SendMessage(string message) = 0;
virtual void SendPicture(Image image) = 0;
virtual ~Messager(){}
};
class MessagerImp{
public:
virtual void PlaySound() = 0;
virtual void DrawShape() = 0;
virtual void WriteText() = 0;
virtual void Connect() = 0;
virtual ~MessagerImp(){}
};
// 平台实现 n
class PCMessagerImp : public MessagerImp{
public:
virtual void PlaySound(){
//...
}
virtual void DrawShape(){
//...
}
virtual void WriteText(){
//...
}
virtual void Connect(){
//...
}
};
class MobileMessagerImp : public MessagerImp{
public:
virtual void PlaySound(){
//...
}
virtual void DrawShape(){
//...
}
virtual void WriteText(){
//...
}
virtual void Connect(){
//...
}
};
//业务抽象 m
class MessagerLite : public Messager{
public:
virtual void Login(string username, stirng passwaed){
messager->Connect();
}
virtual void SendMessage(string message){
messager->WriteText();
}
virtual void SendPicture(Image image){
messager->DrawShape();
}
};
class MessagerPerfect : public Messager{
public:
virtual void Login(string username, stirng passwaed){
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 PCMessagerImp();
Messager* m = new MessagerLite(mImp);
}
类的规模:1 + n + m