Home > Backend Development > PHP Tutorial > Detailed explanation of the adapter pattern of PHP design patterns

Detailed explanation of the adapter pattern of PHP design patterns

韦小宝
Release: 2023-03-17 14:36:01
Original
1694 people have browsed it

Convert the operations of certain similar classes into a unified "interface" (here is a metaphor)--Adapter, or metaphorically as an "interface", unifying or shielding those Class details. Adapter pattern also constructs a "mechanism" so that "adaptive" classes can be easily added or deleted without modifying the code that interacts with Adapter, in line with "reduce code" The design principle of "inter-coupling"

Detailed explanation of the adapter pattern of PHP design patterns

<?php
/*
 * 适配器模式
 */
abstract class Toy
{
    public abstract function openMouth();

    public abstract function closeMouth();
}

class Dog extends Toy
{
    public function openMouth()
    {
        echo "Dog open Mouth\n";
    }

    public function closeMouth()
    {
        echo "Dog close Mouth\n";
    }
}

class Cat extends Toy
{
    public function openMouth()
    {
        echo "Cat open Mouth\n";
    }

    public function closeMouth()
    {
        echo "Cat close Mouth\n";
    }
}


//目标角色:红枣遥控公司
interface RedTarget
{
    public function doMouthOpen();

    public function doMouthClose();
}

//目标角色:绿枣遥控公司及
interface GreenTarget
{
    public function operateMouth($type = 0);
}


//类适配器角色:红枣遥控公司
class RedAdapter implements RedTarget
{
    private $adaptee;

    function __construct(Toy $adaptee)
    {
        $this->adaptee = $adaptee;
    }

    //委派调用Adaptee的sampleMethod1方法
    public function doMouthOpen()
    {
        $this->adaptee->openMouth();
    }

    public function doMouthClose()
    {
        $this->adaptee->closeMouth();
    }
}

//类适配器角色:绿枣遥控公司
class GreenAdapter implements GreenTarget
{
    private $adaptee;

    function __construct(Toy $adaptee)
    {
        $this->adaptee = $adaptee;
    }

    //委派调用Adaptee:GreenTarget的operateMouth方法
    public function operateMouth($type = 0)
    {
        if ($type) {
            $this->adaptee->openMouth();
        } else {
            $this->adaptee->closeMouth();
        }
    }
}


class testDriver //客户端,客户想要那种就实现那种
{
    public function run()
    {
        //实例化一只狗玩具
        $adaptee_dog = new Dog();
        echo "没有适配器的普通模式";
        $adaptee_dog->openMouth();
        $adaptee_dog->closeMouth();
        echo "给狗套上红枣适配器\n";
        $adapter_red = new RedAdapter($adaptee_dog);
        //张嘴
        $adapter_red->doMouthOpen();
        //闭嘴
        $adapter_red->doMouthClose();
        echo "给狗套上绿枣适配器\n";
        $adapter_green = new GreenAdapter($adaptee_dog);
        //张嘴
        $adapter_green->operateMouth(1);
        //闭嘴
        $adapter_green->operateMouth(0);
    }
}

$test = new testDriver();
$test->run();
Copy after login

The adapter pattern converts existing interfaces into interfaces expected by client classes and realizes the reuse of existing classes. It is a A very frequently used design pattern

Related recommendations:

Introduction to the php adapter pattern

Code analysis for adaptations such as PHP adapter mode

Introduction to PHP adapter mode_PHP tutorial

The above is the detailed content of Detailed explanation of the adapter pattern of PHP design patterns. 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