bridge 桥模式

  • 动机
    • 由于某种类型的固有的实现逻辑,使得他们具有两个维度的,乃至多个维度的变化

bridge 桥模式

sample

  1. 在不同的平台上支持相同的功能
  2. 在各自平台上有自己特有的功能
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    class Messager{
    public:
    virtual void Login(string username,string password)=0;
    virtual void SendMessage(string message);
    virtual void SendPicture(Image imagae);

    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{
    virtual void PlaySound(){
    //======
    }

    virtual void DrawShape(){
    //=======
    }

    virtual void WriteText(){
    //======
    }

    virtual void Connect(){
    //======
    }
    }
    // 业务抽象

    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();
    //*******8
    }

    virtual void SendPicture(Image image){
    PCMessagerBase::PlaySound();
    //**********
    PCMessagerBase::DrawShape();
    }
    }

    class PCmessagerLite : public PCmessager{
    public:
    virtual void Login(string username,string password){
    PCmessager::Connect();
    //.....
    }

    virtual void SendMesager(string message){
    PCmessager::WriteText();
    //.....
    }

    virtual void SendPicture(Image iamge){
    PCmessager::DrawShape();
    //.....
    }
    }


    class MobileMessager :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();
    }

    class MobileMessagerLite : public MobileMessagerBase{
    public:
    virtual void Login(string username,string password){
    MobileMessagerBase::Connect();
    //.....
    }

    virtual void SendMesager(string message){
    MobileMessagerBase.WriteText();
    //.....
    }

    virtual void SendPicture(Image iamge){
    MobileMessagerBase.DrawShape();
    //.....
    }
    }
    }

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

改进

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
class MessagerImpl{
public:

virtual void PlaySound()=0;
virtual void DrawShape()=0;
virtual void WriteText()=0;
virtual void Connect()=0;

virtual ~Messager(){}
};
class Messager{
protected:
MessagerImpl* messageimpl;
public:
virtual void Login(string username,string password)=0;
virtual void SendMessage(string message)=0;
virtual void SendPicture(Image imagae)=0;
~MessagerImpl();
}
//平台实现
class PCMessagerImpl : public MessagerImpl{
public:
virtual void PlaySound(){
//******
}

virtual void DrawShape(){
//******
}

virtual void WriteText(){
//******
}

virtual void Connect(){
//******
}
};

class MobileMessagerImpl:public MessagerImpl{
virtual void PlaySound(){
//======
}

virtual void DrawShape(){
//=======
}

virtual void WriteText(){
//======
}

virtual void Connect(){
//======
}
}
// 业务抽象
// 将继承转换为内部类
class MessagerPerfect :public Messager {
public:
virtual void Login(string username,string password){
messageBase->PlaySound();
//*********
messageBase->Connect();
}

virtual void SendMessage(string message){

messageBase->PlaySound();
//**********
messageBase->WriteText();
//*******8
}

virtual void SendPicture(Image image){
messageBase->PlaySound();
//**********
messageBase->DrawShape();
}
MessagerPerfect(Messager* messager):messageBase(messager){}

}

class MessagerLite : public Messager{

public:
virtual void Login(string username,string password){
messageBase->Connect();
//.....
}

virtual void SendMesager(string message){
messageBase->WriteText();
//.....
}

virtual void SendPicture(Image iamge){
messageBase->DrawShape();
//.....
}
MessagerLite(Messager* messager):messageBase(messager){}
}


}

// 运行时装配
void Process(){
MessagerImpl* m =new MessagerLite(new MobileMessagerBase());
MessagerImpl* m =new MessagerLite(new PCMessagerBase());
}

模式定义

将抽象部分(业务功能)与实现部分(平台实现)分离,是他们可以独立的变化

UML图

桥接模式UML

要点总结

  • Bridge 模式使用 “对象间的组合关系” 接耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化。所谓抽象和实现沿着各自维度的变化,即“子类化”他们。
  • Bridge 模式有时候类似于多继承方案,但是多继承往往违背-单一职责(即一个类只有一个变化的原因)。复用性比较差。Bridge 模式是比多继承方案更好的解决方案
  • Bridge 模式的应用一般在 “两个非常强的变化维护” ,有时一个类也有多余两个的变化维度这时可以使用bridge的扩展模式