PHP design pattern-bridging pattern

Source: Internet
Author: User
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 (){}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.