Detailed analysis of adapter pattern in php (with code)

不言
Release: 2023-04-03 18:10:01
Original
2374 people have browsed it

This article brings you a detailed analysis of the adapter mode in PHP (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Adapter pattern (PHP implementation)

e.g.

//Target role

1. The interface has multiple methods

// Source role

1. Only owns some partial methods

//Adapter role

1. Delegate calls to methods that already exist in the source role

2. Suitable Other methods are needed to add clients

//Client

1. Set static method

2. Instantiate source role

3. Through construction The method will be injected into the

Adapter mode (Adapter) mode: convert the interface of one class into the interface of another class that the customer expects. Adapters allow classes with originally incompatible interfaces to work together seamlessly.

[Main roles in the adapter pattern]

Target role: Define the interface used by the client related to a specific domain. This is what we expect

Source (Adaptee) role: The interface that needs to be adapted

Adapter (Adapter) role: Adapt the Adaptee interface and the Target interface; the adapter is the core of this model, and the adapter converts the source interface into the target Interface, this role is a concrete class.

In fact, there is a two-port socket (Adaptee) on the wall of your home, but you bought an electric fan (Target) that requires three ports. In this case, you need an outlet (Adapter).

Class adapters adopt the "multiple inheritance" implementation method, which brings undesirable high coupling, so it is generally not recommended. The object adapter adopts the "object combination" method, which is more in line with the spirit of loose coupling.

The class adapter uses inheritance

e.g.:

/**
 * 目标角色
 */
interface Target {
 
    /**
     * 源类也有的方法1
     */
    public function sampleMethod1();
 
    /**
     * 源类没有的方法2
     */
    public function sampleMethod2();
}
 
/**
 * 源角色
 */
class Adaptee {
 
    /**
     * 源类含有的方法
     */
    public function sampleMethod1() {
        echo &#39;Adaptee sampleMethod1 <br />&#39;;
    }
}
 
/**
 * 类适配器角色
 */
class Adapter extends Adaptee implements Target {
 
    /**
     * 源类中没有sampleMethod2方法,在此补充
     */
    public function sampleMethod2() {
        echo &#39;Adapter sampleMethod2 <br />&#39;;
    }
 
}
 
class Client {
 
    /**
     * Main program.
     */
    public static function main() {
        $adapter = new Adapter();
        $adapter->sampleMethod1();
        $adapter->sampleMethod2();
 
    }
 
}
Copy after login

The object adapter uses delegation

e.g.:

/**
 * 目标角色
 */
interface Target {
 
    /**
     * 源类也有的方法1
     */
    public function sampleMethod1();
 
    /**
     * 源类没有的方法2
     */
    public function sampleMethod2();
}
 
/**
 * 源角色
 */
class Adaptee {
 
    /**
     * 源类含有的方法
     */
    public function sampleMethod1() {
        echo &#39;Adaptee sampleMethod1 <br />&#39;;
    }
}
 
/**
 * 类适配器角色
 */
class Adapter implements Target {
 
    private $_adaptee;
 
    public function __construct(Adaptee $adaptee) {
        $this->_adaptee = $adaptee;
    }
 
    /**
     * 委派调用Adaptee的sampleMethod1方法
     */
    public function sampleMethod1() {
        $this->_adaptee->sampleMethod1();
    }
 
    /**
     * 源类中没有sampleMethod2方法,在此补充
     */
    public function sampleMethod2() {
        echo &#39;Adapter sampleMethod2 <br />&#39;;
    }
 
}
 
class Client {
 
    /**
     * Main program.
     */
    public static function main() {
        $adaptee = new Adaptee();
        $adapter = new Adapter($adaptee);
        $adapter->sampleMethod1();
        $adapter->sampleMethod2();
 
    }
 
}
Copy after login

Related recommendations :

How does php generate classes for HTML files? How to generate html file class in php

How to debug PHP code? Summary of PHP code debugging methods (recommended)

The above is the detailed content of Detailed analysis of adapter pattern in php (with code). 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!