What is the adapter pattern and what are its application scenarios?

齐天大圣
Release: 2023-04-09 12:38:01
Original
5183 people have browsed it

The adapter pattern is a structural design pattern and it is also a commonly used design pattern.

Adapter pattern

The adapter pattern is easy to understand. From the literal meaning, we know that it is used for adaptation. Its role is to convert the originally incompatible interface into the interface expected by the customer, so that classes that cannot work together due to incompatible interfaces can work together. To explain with an example from daily life, we know that there are many types of mobile phone charging ports, large holes and small holes for Apple and Android. Often when our phone is out of power and we borrow a charger, we find that it is not compatible with our own phone hole. Now there are adapters with chargers on the market. With the adapters, we can charge our mobile phones.

There are three roles in the adapter: target abstract class (Target), adapter (Adaptee), adapter (Adaptor)

Class adapter and object adapter

Adapters are divided into class adapters and object adapters. Let’s explain them through an example.

The following is an example of a class adapter:

class ITarget
{
    function f1();
    function f2();
    function f3();
}

class Adaptee
{
    public function f1(){……};
    public function f2(){……};
    public function a(){……};
}

// 类适配器,基于继承
class Adaptor extends Adaptee
{
    public function f3 () {
        // 重新实现f3
    }
}
Copy after login

The following is an example of an object adapter

class ITarget
{
    function f1();
    function f2();
    function f3();
}

class Adaptee
{
    public function a1(){……};
    public function b1(){……};
    public function c1(){……};
}

// 对象适配器,基于组合
class Adaptor 
{
    private $adaptee = null;
    
    public function __contrust (Adaptee $adaptee)
    {
        $this->adaptee = $adaptee;
        ……
    }
    
    public function f1() {
        $this->adaptee->a1();
    }
    
    public function f2() {
        // 重新实现f2
    }
    
    public function f3 () {
        $this->adaptee->a3();
    }
}
Copy after login

How should we choose between these two implementation methods. If most of the methods in the adapter can be reused, then choose to use a class adapter, otherwise use an object adapter. Object adapters are more flexible.

Application Scenarios

The following is an introduction to the main usage scenarios of the adapter

  • Encapsulation is defective Interface design

  • Unify the interface design of multiple classes. For example, a payment system has three different payment methods, WeChat payment, Alipay payment, and online banking payment. The implementation methods are all different, so we can use the adapter pattern to let them have a unified method for external use. In this way, it is very convenient for us to call.

  • Interface compatible with old versions, this is often used in our system upgrades.

The above is the detailed content of What is the adapter pattern and what are its application scenarios?. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!