What is adapter mode?
Adapter Pattern
The Adapter pattern is also called the adapter pattern. It is one of the structural patterns. Existing classes can be changed through the Adapter pattern ( or external class) interface form.
Adapter pattern application scenarios
In the process of large-scale system development, we often encounter situations such as the following:
We need to implement a certain These functions already have one or more external components that are not yet mature. If we re-develop these functions ourselves, it will take a lot of time; so in many cases we will choose to use external components temporarily and then consider replacing them at any time later. But this will bring about a problem. With the replacement of the external component library, it may be necessary to make extensive modifications to the source code that references the external component, so it is very likely to introduce new problems and so on. How to minimize the modification area? The Adapter pattern was proposed for this similar need. The Adapter pattern transparently calls external components by defining a new interface (which abstracts the functions to be implemented) and an Adapter class that implements the interface. In this way, when replacing external components, you only need to modify a few Adapter classes at most, and other source codes will not be affected.
php example
Suppose we have an article class that has completed the list of articles and detailed information display:
<?php class article{ //文章列表获取方法 public function getLIst(){ echo '获取文章列表'; } //根据文章id获取文章的标题和内容 public function getInfo($id){ echo '根据文章id获取文章的标题和内容'; } } $art = new article(); $art->getInfo(1);
Due to the needs of the project, Such a function is now needed. When obtaining article details, you also need to obtain the creation time of the article and update the number of times the article has been read.
If we do not use the adapter mode, our first thought is to modify the source code of the article class to add such a function. The reason why I think this is because the example code above is very simple. What if it is thousands of lines? What if the getInfo() code is very complex?
Use the adapter pattern to solve all this!
class articleAdapter{ public $_artObj; public function __construct($artObj){ $this->_artObj = $artObj; } public function getInfo($id){ $this->_artObj->getInfo($id); } public function getInfoAndUpdate($id){ //利用$this->_artObj查询符合要求的文章数据并更新浏览次数 echo '$this->_artObj查询符合要求的文章数据并更新浏览次数'; } } $art = new articleAdapter(new article()); $art->getInfo(12); $art->getInfoAndUpdate(12);
Why not use inheritance?
Object adapter: It is not processed through inheritance, but through object combination. As long as we have studied OO design principles, we all know that combination is the recommended way compared to inheritance. .
Class adapter: implemented through inheritance, encapsulating the methods of the old system. In the process of converting object adapters between adapters, class adapters can undoubtedly be completed, but the dependencies will increase, and with the flexibility of adaptation requirements, the expansion may become difficult to control through inheritance.
Generally speaking, class adapters are less flexible, while object adapters are more flexible. This is the recommended method. It can be done through dependency injection or configuration. The class adapter needs to inherit from the class of the old system to be adapted, which is undoubtedly not a good idea.
The above is the detailed content of What is adapter mode?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
