Simple analysis of PHP adapter mode (Adapter) _php instance

Source: Internet
Author: User

The first few introduced the characteristics of design patterns and explained in detail the 4 creation patterns, which are responsible for generating object instances, followed by architectural patterns.

first, what is the structure of the model ?

The structure pattern is the internal structure and the external combination of the Analytic class and object, and solves the coupling problem between modules by optimizing the program structure.

second, the type of structural model :

Adapter mode
Bridging mode
Decoration mode
Combination mode
Appearance mode
Enjoy meta mode
Agent mode

1. Adapter Mode (Adapter)
Converts the interface of a class into another interface that the customer wants, and the adapter pattern makes it possible for those classes that cannot work together because the interface is incompatible.
Application scenario: The old code interface does not adapt to the new interface requirements, or the code is too messy to continue to modify, or use a Third-party class library.

Code implementation

Copy Code code as follows:

The old Code
Class User {
Private $name;
function __construct ($name) {
$this->name = $name;
}
Public Function GetName () {
return $this->name;
}
}

New Code, open platform standard interface
Interface UserInterface {
function GetUserName ();
}
Class UserInfo implements UserInterface {
protected $user;
function __construct ($user) {
$this->user = $user;
}
Public Function GetUserName () {
return $this->user->getname ();
}
}

$olduser = new User (' John ');
echo $olduser->getname (). n ";
$newuser = new UserInfo ($olduser);
echo $newuser->getusername (). n ";

Note: The new interface here uses the combination method, the userinfo inside has a member variable to save the old interface user object, the module is loose coupling, this kind of structure is in fact the combination mode. Do not use inheritance, although userinfo inherits user can achieve the same goal, but the coupling degree is high, the mutual influence.

The above is about the PHP design pattern in the structural mode of the entire content of the adapter mode, the small partners whether understand it, if you have a problem, give me a message

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.