Object-oriented advanced design pattern: Adapter pattern

巴扎黑
Release: 2023-03-07 19:44:02
Original
1815 people have browsed it

What is the adapter pattern?

The adapter pattern simply adapts the interface of an object to the interface expected by another object.

Adapter pattern application problems and solutions

In your application, you may use a working code base that is architecturally reliable and stable. But we often add new functionality that requires existing objects to be used in a different way than how they were originally designed. At this point, the obstacle may simply be that the new feature needs a different name. In more complex scenarios, an obstacle may also be that the new functionality requires slightly different behavior than the original object.

In response to the above problems, the solution we adopted is to use the adapter pattern to build another object. This Adapter object acts as an intermediary between the original application and the new functionality. The adapter pattern defines new interfaces for existing objects to match the requirements of new objects.

Question

Assume that the functions of the Alipay payment class are as follows:

/** 
 * 支付宝支付类 
 */  
class Alipay  
{  
    public function sendPayment()  
    {  
        echo '使用支付宝支付。';  
    }  
}  
  
// 客户端代码  
$alipay = new Alipay();  
$alipay->sendPayment();
Copy after login

We directly instantiate the Alipay class to complete the payment function. Such client code may a lot of.

After a period of time, what will happen if Alipay’s Alipay class is upgraded and the method name changes from sendPayment() to goPayment()?

All client code that uses sendPayment() must be changed.

If the Alipay class is frequently upgraded, or the client is used in many places, this will be a huge workload.

Solution

Now we use the adapter pattern to solve it.

We add an intermediate class, that is, the adapter class, between the client and the Alipay class to convert the original Alipay into the form required by the client.

In order to allow the client to call a unified class method, we first define an adapter interface:

/** 
 * 适配器接口,所有的支付适配器都需实现这个接口。 
 * 不管第三方支付实现方式如何,对于客户端来说,都 
 * 用pay()方法完成支付 
 */  
interface PayAdapter  
{  
    public function pay();  
}
Copy after login

Because we have no control over the Alipay class, and it may be updated frequently, we do not modify it Make any changes.

We create a new AlipayAdapter adapter class and convert Alipay's payment function in pay(), as follows:

/** 
 * 支付宝适配器 
 */  
class AlipayAdapter implements PayAdapter  
{  
    public function pay()  
    {  
        // 实例化Alipay类,并用Alipay的方法实现支付  
        $alipay = new Alipay();  
        $alipay->sendPayment();  
    }  
}
Copy after login

Client usage:

// 客户端代码  
$alipay = new AlipayAdapter();  
// 用pay()方法实现支付  
$alipay->pay();
Copy after login

In this way, when Alipay's If the payment method changes, you only need to modify the AlipayAdapter class.

Adapting new classes

With adapters, expansion becomes easier.

Continuing with the above example, on the basis of Alipay, we will add WeChat payment. It is different from Alipay's payment method and must be paid by scanning the QR code.

In this case, you should also use an adapter instead of directly using WeChat’s payment function.

The code is as follows:

/** 
 * 微信支付类 
 */  
class WechatPay  
{  
    public function scan()  
    {  
        echo '扫描二维码后,';  
    }  
  
    public function doPay()  
    {  
        echo '使用微信支付';  
    }  
}  
  
/** 
 * 微信支付适配器 
 */  
class WechatPayAdapter implements PayAdapter  
{  
    public function pay()  
    {  
        // 实例化WechatPay类,并用WechatPay的方法实现支付。  
        // 注意,微信支付的方式和支付宝的支付方式不一样,但是  
        // 适配之后,他们都能用pay()来实现支付功能。  
        $wechatPay = new WechatPay();  
        $wechatPay->scan();  
        $wechatPay->doPay();  
    }  
}
Copy after login

Client usage:

// 客户端代码  
$wechat = new WechatPayAdapter();  
// 也是用pay()方法实现支付  
$wechat->pay();
Copy after login

This is the extended feature of the adapter.

We created a method for handling third-party classes (Alipay, WeChat Pay).

If their APIs change, we only need to modify the adapter class that the client depends on. , without modifying or exposing the third-party class itself.

UML diagram

The code of the above adapter pattern corresponds to UML as follows:

Object-oriented advanced design pattern: Adapter pattern

Summary

Large applications will continue to add new libraries and new APIs.

In order to avoid problems caused by their changes, they should be packaged in the adapter pattern to provide a unified reference method for the application.

It will make our code more structured and easier to manage and expand.

The above is the detailed content of Object-oriented advanced design pattern: Adapter pattern. For more information, please follow other related articles on the PHP Chinese website!

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!