An analysis of PHP adapter mode (Adapter) _php instance

Source: Internet
Author: User
The first few introduced the design pattern characteristics and explained in detail 4 kinds of creation pattern, the creation pattern is responsible for how to produce the object instance, next talk about the structure pattern.

first, what is the structural model ?

Structural pattern is the internal structure and external combination of analytic classes and objects, and solves the coupling problem between modules by optimizing the program structure.

ii. types of structural patterns :

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

1. Adapter Mode (Adapter)
Converting a class's interface to another interface that the customer wants, the adapter pattern makes it possible for those classes that are incompatible with the interface to work together.
Application scenario: The old code interface does not adapt to the new interface requirements, or the code is much too messy to continue to modify, or use a third-party class library.

Code implementation

Copy the 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 (' Zhang San ');
echo $olduser->getname (). " n ";
$newuser = new UserInfo ($olduser);
echo $newuser->getusername (). " n ";

Note: The new interface here uses a combination, userinfo inside a member variable to save the old interface user object, the module is loosely coupled, this structure is actually a combination mode. Do not use inheritance, although the UserInfo inherits the user to achieve the same goal, but the coupling degree is high, each other has the influence.

The above is about the PHP design mode in the structural mode of the adapter mode of the entire content, whether the small partners understand clearly, if you have any questions, please leave me a message

  • Related Article

    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.