PHP design pattern-Bridge pattern the Bridge pattern belongs to the structural pattern. what is the structural pattern? that is, the structural pattern solves the coupling problem between modules (low coupling) from the structure of the program)
Overview: separates abstract parts from their implementations so that they can all change independently.
Bridging mode:
Separate the abstract part from its implementation part so that they can all change independently
Abstraction is separated from its implementation, that is, the abstract class and its derived class are used to implement their own objects.
Relationship between bridging and adapter mode (explained later)
Bridging is an aggregation relationship. The two are associated but not inherited.
The adapter belongs to the combination relationship, and the adapter must inherit the source
Aggregation: Object A can contain object B, but object B is not part of object.
Personal example:
1. geese and geese
2. mobile phone software and brand features
Composition:
Relationship between birds and wings
Namespace haibao \ design \ web \ view \ design;
Use haibao \ design \ web \ common \ design \ bridge \ PhoneBrandNokia;
Use haibao \ design \ web \ common \ design \ bridge \ PhoneBrandApple;
Use haibao \ design \ web \ common \ design \ bridge \ PhoneGame;
Use haibao \ design \ web \ common \ design \ bridge \ PhoneList;
Class Bridge extends \ haibao \ design \ web \ view \ Base {
Protected function preRender (){
Header ("Content-type: text/html; charset = utf-8 ");
$ Nokia = new PhoneBrandNokia ();
$ Nokia-> setPhoneSoft (new PhoneGame ('Nokia '));
$ Nokia-> run ();
Echo'
';
$ Nokia-> setPhoneSoft (new PhoneList ('Nokia '));
$ Nokia-> run ();
Echo'
';
$ Apple = new PhoneBrandApple ();
$ Apple-> setPhoneSoft (new PhoneGame ('apple '));
$ Apple-> run ();
Echo'
';
$ Apple-> setPhoneSoft (new PhoneList ('apple '));
$ Apple-> run ();
Echo'
';
}
}
/**
* Mobile phone brand
*/
Namespace haibao \ design \ web \ common \ design \ bridge;
Abstract class PhoneBrand {
Public $ phoneSoft;
Public function setPhoneSoft ($ phoneSoft ){
$ This-> phoneSoft = $ phoneSoft;
}
Public function run (){}
}
/**
* Apple
*/
Namespace haibao \ design \ web \ common \ design \ bridge;
Class PhoneBrandApple extends PhoneBrand {
Public function run (){
$ This-> phoneSoft-> run ();
}
}
/**
* Nokia
*/
Namespace haibao \ design \ web \ common \ design \ bridge;
Class PhoneBrandNokia extends PhoneBrand {
Public function run (){
$ This-> phoneSoft-> run ();
}
}
/**
* Mobile games
*/
Namespace haibao \ design \ web \ common \ design \ bridge;
Class PhoneGame extends PhoneSoft {
Public $ name;
Public function _ construct ($ name ){
$ This-> name = $ name;
}
Public function run (){
Echo 'run'. $ this-> name. 'platform mobile games ';
}
}
/**
* Mobile phone address book
*/
Namespace haibao \ design \ web \ common \ design \ bridge;
Class PhoneList extends PhoneSoft {
Public $ name;
Public function _ construct ($ name ){
$ This-> name = $ name;
}
Public function run (){
Echo 'run'. $ this-> name. 'phone address book ';
}
}
/**
* Mobile software
*/
Namespace haibao \ design \ web \ common \ design \ bridge;
Abstract class PhoneSoft {
Public function run (){}
}