其他分享
首页 > 其他分享> > 桥模式

桥模式

作者:互联网

在软件组织中,责任划分不明确,使得继承会使得子类随着需求变化而急剧膨胀

以下这种消息的基类进行扩展,出现pc和mobile并出现了lite和prefect版本其中的功能(方法)实现代码大量重复

将基类分成两部分抽象部分(业务功能)和实现部分(平台实现)

bridge1.cpp

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 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(){
        //==========
    }
};



//业务抽象

class PCMessagerLite : public PCMessagerBase {
public:
    
    virtual void Login(string username, string password){
        
        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, string password){
        
        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, string password){
        
        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, string password){
        
        MobileMessagerBase::PlaySound();
        //********
        MobileMessagerBase::Connect();
        //........
    }
    virtual void SendMessage(string message){
        
        MobileMessagerBase::PlaySound();
        //********
        MobileMessagerBase::WriteText();
        //........
    }
    virtual void SendPicture(Image image){
        
        MobileMessagerBase::PlaySound();
        //********
        MobileMessagerBase::DrawShape();
        //........
    }
};


void Process(){
        //编译时装配
        Messager *m =
            new MobileMessagerPerfect();
}

 

bridge2.cpp

  1 class Messager{
  2 protected:
  3      MessagerImp* messagerImp;//...
  4 public:
  5     virtual void Login(string username, string password)=0;
  6     virtual void SendMessage(string message)=0;
  7     virtual void SendPicture(Image image)=0;
  8     
  9     virtual ~Messager(){}
 10 };
 11 
 12 class MessagerImp{
 13 public:
 14     virtual void PlaySound()=0;
 15     virtual void DrawShape()=0;
 16     virtual void WriteText()=0;
 17     virtual void Connect()=0;
 18     
 19     virtual MessagerImp(){}
 20 };
 21 
 22 
 23 //平台实现 n
 24 class PCMessagerImp : public MessagerImp{
 25 public:
 26     
 27     virtual void PlaySound(){
 28         //**********
 29     }
 30     virtual void DrawShape(){
 31         //**********
 32     }
 33     virtual void WriteText(){
 34         //**********
 35     }
 36     virtual void Connect(){
 37         //**********
 38     }
 39 };
 40 
 41 class MobileMessagerImp : public MessagerImp{
 42 public:
 43     
 44     virtual void PlaySound(){
 45         //==========
 46     }
 47     virtual void DrawShape(){
 48         //==========
 49     }
 50     virtual void WriteText(){
 51         //==========
 52     }
 53     virtual void Connect(){
 54         //==========
 55     }
 56 };
 57 
 58 
 59 
 60 //业务抽象 m
 61 
 62 //类的数目:1+n+m
 63 
 64 class MessagerLite :public Messager {
 65 
 66     
 67 public:
 68     
 69     virtual void Login(string username, string password){
 70         
 71         messagerImp->Connect();
 72         //........
 73     }
 74     virtual void SendMessage(string message){
 75         
 76         messagerImp->WriteText();
 77         //........
 78     }
 79     virtual void SendPicture(Image image){
 80         
 81         messagerImp->DrawShape();
 82         //........
 83     }
 84 };
 85 
 86 
 87 
 88 class MessagerPerfect  :public Messager {
 89     
 90    
 91 public:
 92     
 93     virtual void Login(string username, string password){
 94         
 95         messagerImp->PlaySound();
 96         //********
 97         messagerImp->Connect();
 98         //........
 99     }
100     virtual void SendMessage(string message){
101         
102         messagerImp->PlaySound();
103         //********
104         messagerImp->WriteText();
105         //........
106     }
107     virtual void SendPicture(Image image){
108         
109         messagerImp->PlaySound();
110         //********
111         messagerImp->DrawShape();
112         //........
113     }
114 };
115 
116 
117 
118 
119 void Process(){
120     //运行时装配
121     MessagerImp* mImp=new PCMessagerImp();
122     Messager *m =new Messager(mImp);
123 }

 

标签:string,void,virtual,模式,........,PlaySound,public
来源: https://www.cnblogs.com/miaorn/p/14396842.html