Home >Backend Development >PHP Tutorial >Let's take a look at the adapter pattern of PHP design patterns

Let's take a look at the adapter pattern of PHP design patterns

coldplay.xixi
coldplay.xixiforward
2020-06-30 17:42:342840browse

Let's take a look at the adapter pattern of PHP design patterns

PHP Design Pattern Adapter Pattern

  1. Introduction of life scene problems:

If you need to When traveling and playing abroad, there are now a lot of electronic products, but every electronic product needs a power source to drive it, such as mobile phones, computers, and cameras. . . But there is another very obvious problem. Electronic products consume a lot of power. When the power is gone, they need to be recharged. In our country, the power chargers that come with the electronic products we buy are generally 220V. , but the world is very big, and the requirements (regulations)
of each country are different. For example, the United States is 120V

    Principle:
Convert a class The interface is converted into another interface, so that classes whose original interfaces are incompatible can be compatible.

The user calls the target interface method converted by the adapter, and the adapter then calls the relevant interface method of the adapted person

    Benefits:
The user is The adaptee cannot be seen, and it is a decoupled operation

  1. The adapter pattern is a structural design pattern

  2. Three types of adapter patterns Implementation method

1. Class adapter

According to the UML diagram above, we can see that, Requires a US voltage interface

UsaInterface, an adapter class Adapter, a Chinese voltage interface Chine, a user Computer,Here, the Chinese voltage is the adaptee, and the American voltage is the final output (target), which is the configurer

interface UsaInterface{
    public function outPut5V();}class China{
    private $voltage = 220;

    public function outPut220V()
    {
        return $this->voltage;
    }}class Adapter extends China implements UsaInterface{

    public function outPut5V()
    {
        // TODO: Implement outPut5V() method.
        $chinaVoltage = $this->outPut220V();

        return $chinaVoltage / 2 + 10 . "V";
    }}class Computer{
    public function index(UsaInterface $usa)
    {
        if ($usa->outPut5V() === "120V") {
            printf("可以在美国使用中国的电源插头给电脑充电了");
        } else {
            printf("不可以在美国使用中国的电源插头给电脑充电了");
        }
    }}class Client{
    public function __construct()
    {
        $adapter = new Adapter();
        $computer = new Computer();

        $computer->index($adapter);
    }}

2. Object Adapter Mode

Object adapter is actually an adjustment on the class adapter model, so that the adapter class

Adapter does not inherit the adapted class, but allows them to be combined together, so that the use can be increased The degree of flexibility will not expose many methods of the adaptee (use association relationships instead of inheritance relationships [synthetic reuse principle]). At the same time, without inheritance, there is no way to override the method rewriting of the parent class, which can be considered a good thing and a bad thing. As follows:

class Adapter implements UsaInterface{
    protected $china;

    public function __construct(China $china)
    {
        $this->china = $china;
    }


    public function outPut5V()
    {
        // TODO: Implement outPut5V() method.
        $chinaVoltage = $this->china->outPut220V();

        return $chinaVoltage / 2 + 10 . "V";
    }}

3. Interface Adapter (Default Adapter Mode)

When you do not need to implement all the methods provided by the interface, you can first design an abstract class to implement the interface , and provides a default empty method implementation for the interface, then changing the sub-method of the abstract class can selectively override certain methods of the parent class to meet the requirements (applicable to situations where an interface does not want to use all methods )


Related learning recommendations:

PHP programming from entry to proficiency

The above is the detailed content of Let's take a look at the adapter pattern of PHP design patterns. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete