A brief analysis of php adapter mode (Adapter), a brief analysis of adapter_PHP tutorial

WBOY
Release: 2016-07-13 10:13:19
Original
1273 people have browsed it

A brief analysis of php adapter mode (Adapter), a brief analysis of adapter

The previous articles introduced the characteristics of design patterns and explained in detail the four creational patterns. The creational pattern is responsible for how to generate object instances. Next, we will talk about the structural pattern.

1. What is a structural pattern?

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

2. Types of structural patterns :

Adapter Mode
Bridge Mode
Decoration Mode
Combination mode
Appearance Mode
Flyweight mode
Proxy mode

1. Adapter mode (Adapter)
By converting the interface of a class into another interface that the client wants, the adapter pattern enables classes that originally could not work together due to incompatible interfaces to work together.
Application scenarios: The old code interface does not meet the new interface requirements, or the code is too messy to continue to modify, or a third-party class library is used.

Code implementation

Copy code The code is as follows:

//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 the combination method. There is a member variable inside UserInfo to save the old interface User object. The modules are loosely coupled. This structure is actually the combination mode. Do not use inheritance. Although UserInfo can achieve the same purpose by inheriting User, the coupling is high and they will affect each other.

The above is all about the adapter pattern in the structural pattern in the PHP design pattern. Do you guys understand it clearly? If you have any questions, please leave me a message

http://www.bkjia.com/PHPjc/917026.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/917026.htmlTechArticleA brief analysis of the php adapter pattern (Adapter), a brief analysis of the adapter The previous articles introduced the characteristics of the design pattern and explained it in detail There are 4 creative patterns. The creative pattern is responsible for how to generate object instances...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template