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