Adapter Mode and PHP implementation

Source: Internet
Author: User

1. Overview

Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible to work together those classes that would otherwise not work together because of incompatible interfaces.

2. Issues to solve

That is, the adapter mode makes it possible for those classes that cannot work together because the interface is incompatible.

3. Roles in the pattern

3.1 Destination Interface (target): the interface that the customer expects. The target can be a specific or abstract class, or it can be an interface.

3.2 Source Interface/Class (Adaptee): A class or adapter class that needs to be adapted.

3.3 Adapter (Adapter): Converts the source interface into a target interface by wrapping an object that needs to be adapted.

4. UML Diagram:

  

5 advantages

5.1.1 through the adapter, the client can invoke the same interface, which is therefore transparent to the client. This is simpler, more straightforward, and more compact.

5.1.2 the existing classes and solves the problem that the existing class and reuse environment requirements are inconsistent.

5.1.3 Decouples the target class from the adapter class by introducing an adapter class to reuse existing adapter classes without modifying the original code.

5.1.41 object adapters can adapt multiple different adapter classes to the same target, that is, the same adapter can be used to match the matching class and its subclasses to the target interface.

6 Disadvantages

For an object adapter, the implementation of the replacement adapter is more complex.

7 Applicable scenarios

7.1 The system needs to use existing classes, and the interfaces of these classes do not conform to the interface of the system.

7.2 Want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future.

7.3 Two classes do things the same or similar, but with different interfaces.

7.4 The old system-developed class has implemented some functionality, but the client can only access it as a different interface, but we don't want to change the original class manually.

7.5 Using third-party components, the component interface definition differs from its own definition and does not want to modify its own interface, but uses the functionality of the third-party component interface.

8 Code implementation:

<?PHP//target ObjectInterfacetarget{function mothed ();}//Source InterfaceInterfaceadaptee{function mothed ();}classadaptee implements{function mothed () {echo"Source Method"; }}//AdapterclassAdapter Implements target{Private$adaptee; function __construct (Adaptee $adaptee) {$ This->adaptee =$adaptee; }    //The method of the override target interface executes the method of the source interface from the implementation adaptationfunction mothed () {$ This->adaptee->methed (); }}

9: Specific examples:

<?PHP//The code has two interfaces, respectively, the German standard interface and GB interface, respectively named Dbsocketinterface and Gbsocketinterface, in addition to two implementation classes, respectively, the German socket and China socket,
They are dbsocket and Gbsocket respectively. To provide a fit between two sets of interfaces, we provide an adapter called Socketadapter. In addition, there is a client, for example, we travel to Germany
When staying at a hotel, called Hotel, in this German hotel to use the German interface. German standard interface:Interfacedbsocketinterface{/** * The name of this method is called: the use of two round head socket power supply * My English on this level*/ functionpowerwithtworound ();}//German socket for Germany standard interfaceclassDbsocketImplementsdbsocketinterface{ Public functionPowerwithtworound () {Echo"Power supply with two round-head jacks"; }}//German hotel is a client, it has a German standard interface, you can use the German standard interface to charge the phone:classhotel{//There's a German standard Jack in the hotel. Private $dbSocket; Public functionHotel (dbsocketinterface$dbSocket) { $this->dbsocket =$dbSocket; } Public functionSetsocket (dbsocketinterface$dbSocket){ $this->dbsocket =$dbSocket; } //There is a charging function in the hotel Public functioncharge () {//charging with a German standard socket $this->dbsocket->Powerwithtworound (); }}//now write a piece of code to test:classTest { Public Static functionMain () {//Initializes a German socket object, referencing it with a German standard interface $dbSoket=NewDbsocket (); //Create a hotel object $hotel=NewHotel ($dbSoket); //charge your phone in a hotel $hotel-charge (); }}//run the program and print out the following results: power supply using two round head jacks//Now I'm going to Germany to travel with three flat-head mobile phone chargers. If I don't have a power adapter, I can't recharge it because it's impossible to change the wall socket for me for a traveler,
More impossible for me to cover a special use of China's national standard socket hotel. Because the Germans have been so used, and very good use, as the saying goes in Rome, I will find a way to solve the problem.
Corresponding to our code, that is, the above Hotel class, Dbsocket class, Dbsocketinterface interface are immutable (provided by customers in Germany), if I want to use this set of APIs, then I can only write code to solve. The following is the GB interface and China socket code. GB Interface:InterfaceGbsocketinterface {/** * The name of this method is called: Power supply using three-item flat-head sockets **/ functionPowerwiththreeflat ();}//China socket to achieve GB interface:classGbsocketImplementsgbsocketinterface{ Public functionPowerwiththreeflat () {Echo"Power supply using three-item flat-head jacks"; }}//I can think of these two things I brought to Germany, and they are not yet available because the interface is different. Then I have to create an adapter that must meet the following criteria://1 must conform to the German standard interface, otherwise there is no way to plug into the German socket; 2 when charging the German standard interface implemented above, a mechanism is provided to transfer the call to the GB interface. This requires: The 1 adapter must implement the original old interface 2 The adapter object holds a reference to the new interface, when the old interface is called, the call is delegated to the implementation of the new interface object to handle, that is, the adapter object to combine a new interface. The implementation of the adapter class is given below:classSocketadapterImplementsdbsocketinterface{//implementing legacy interfaces//combining new interfaces Private $gbSocket; /** * When creating an adapter object, you must pass in a Xinjiekou implementation class **/ Public functionSocketadapter (gbsocketinterface$gbSocket) { $this->gbsocket =$gbSocket; } /** * Will adapt the call to the interface to the new interface*/ Public functionPowerwithtworound () {$this->gbsocket->Powerwiththreeflat (); }}//this adapter class satisfies the above two requirements. Here is a test code to verify that the adapter can work, we follow the step by step to write the code, to clearly explain how the adapter is used. 1 I went to Germany to travel, with the charger is the national standard (can be gbsocket here as a charger)$gbSocket=NewGbsocket ();//Secondly, to Germany, find a German hotel to stay (this hotel or the above code in the hotel, using the German standard socket)$hotel=NewHotel ();//3 due to the inability to recharge, I took out the adapter I took with me and plugged the charger I brought into the upper jack of the adapter. This upper jack is in accordance with the national standard, my charger can be inserted completely. $socketAdapter=NewSocketadapter ($gbSocket);//4 Insert the lower end of the adapter into the socket on the hotel$hotel->setsocket ($socketAdapter);//5 The adapter can be used to charge the hotel$hotel-charge ();//the above five steps are the use of the adapter process, the following is the complete test code. classTestadapter { Public Static functionMain () {$gbSocket=NewGbsocket (); $hotel=NewHotel (); $socketAdapter=NewSocketadapter ($gbSocket); $hotel->setsocket ($socketAdapter); $hotel-charge (); }}?>

Adapter Mode and PHP implementation

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.