PHP design mode-Adapter mode

Source: Internet
Author: User
PHP design mode: Adapter mode: structural mode

Overview: converts an interface of a class to another interface that the customer wants. The Adapter mode makes those classes that cannot work together due to interface incompatibility work together.

Prerequisites:

Main role in the adapter

Target role: defines the interface used by the client for a specific domain, which is what we want

Source (Adaptee) role: interface to be adapted

Adapter role: The Adaptive Adaptee interface is adapted to the Target interface. The Adapter is the core of this mode. the Adapter converts the source interface to the Target interface. this role is a specific class.

Adapter use cases

1. you want to use an existing class, and its interface does not meet your needs.

2. you want to create a reusable class that can work collaboratively with other unrelated or unpredictable classes.

3. you want to use an existing subclass, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface (only for the object adapter)

Personal example:

1. Yao plays basketball in the NBA but does not understand English. a translator is required to understand tactics.

2. The iPhone uses the charging line of the Android phone.

Namespace haibao \ design \ web \ view \ design;

Use haibao \ design \ web \ common \ design \ adapter \ Forwards;

Use haibao \ design \ web \ common \ design \ adapter \ Center;

Use haibao \ design \ web \ common \ design \ adapter \ Translator;

Class Adapter extends \ haibao \ design \ web \ view \ Base {

Protected function preRender (){

Header ("Content-type: text/html; charset = utf-8 ");

$ Forwards = new Forwards ('Kobe ');

$ Forwards-> attack ();

$ Forwards-> defense ();

Echo"

";

$ Center = new Center ('Howard ');

$ Center-> attack ();

$ Center-> defense ();

Echo"

";

$ Translator = new Translator ('Yao Ming ');

$ Translator-> attack ();

$ Translator-> defense ();

Echo"

";

}

}

/**

* Center

*/

Namespace haibao \ design \ web \ common \ design \ adapter;

Class Center extends Player {

Public function _ construct ($ name ){

Parent: :__ construct ($ name );

}

Public function attack (){

Echo 'center'. $ this-> name. 'attack
';

}

Public function defense (){

Echo 'center'. $ this-> name. 'defense
';

}

}

/**

* Foreign striker

*/

Namespace haibao \ design \ web \ common \ design \ adapter;

Class ForeignCenter {

Private $ name;

Public function _ construct ($ name ){

$ This-> name = $ name;

}

Public function chinaattack (){

Echo 'foreign forward '. $ this-> name.' attack
';

}

Public function chinadefense (){

Echo 'foreign forward '. $ this-> name.' defense
';

}

}

/**

* Forward

*/

Namespace haibao \ design \ web \ common \ design \ adapter;

Class Forwards extends Player {

Public function _ construct ($ name ){

Parent: :__ construct ($ name );

}

Public function attack (){

Echo 'Forward '. $ this-> name.' attack
';

}

Public function defense (){

Echo 'Forward '. $ this-> name.' defense
';

}

}

/**

* Players

*/

Namespace haibao \ design \ web \ common \ design \ adapter;

Abstract class Player {

Public $ name;

Public function _ construct ($ name ){

$ This-> name = $ name;

}

Public function attack (){

}

Public function defense (){

}

}

/**

* Foreign center translators

*/

Namespace haibao \ design \ web \ common \ design \ adapter;

Class Translator extends Player {

Public $ foreignCenter;

Public function _ construct ($ name ){

$ This-> foreignCenter = new ForeignCenter ($ name );

}

Public function attack (){

$ This-> foreignCenter-> chinaattack ();

}

Public function defense (){

$ This-> foreignCenter-> chinadefense ();

}

}

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.