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