Adapter mode of PHP design pattern (Adapter) Code examples (10)

purpose

The interface of a certain class is converted to another interface for compatibility. The adapter allows classes that cannot be used together because of different interfaces to work together.

The adapter provides a compatible interface to the user by converting the original interface.

example

Client database adapter

Use multiple different network services and adapters to standardize the data so that the results are the same

UML graphics

★The advanced knowledge system of BAT and other major manufacturers is ready (relevant learning materials and interview questions) and not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx and other knowledge points advanced advanced dry goods​

Code

  • BookInterface.php

<?php

namespace DesignPatterns\Structural\Adapter;

interface BookInterface
{
    public function turnPage();

    public function open();

    public function getPage(): int;
}




  • Book.php

<?php

namespace DesignPatterns\Structural\Adapter;

class Book implements BookInterface
{
    /**
    * @var int
    */
    private $page;

    public function open()
    {
        $this->page = 1;
    }

    public function turnPage()
    {
        $this->page++;
    }

    public function getPage(): int
    {
        return $this->page;
    }
}
  • EBookAdapter.php

<?php

namespace DesignPatterns\Structural\Adapter;

/**
* 这里是一个适配器. 注意他实现了 BookInterface,
* 因此你不必去更改客户端代码当使用 Book
*/
class EBookAdapter implements BookInterface
{
    /**
    * @var EBookInterface
    */
    protected $eBook;

    /**
    * @param EBookInterface $eBook
    */
    public function __construct(EBookInterface $eBook)
    {
        $this->eBook = $eBook;
    }

    /**
    * 这个类使接口进行适当的转换.
    */
    public function open()
    {
        $this->eBook->unlock();
    }

    public function turnPage()
    {
        $this->eBook->pressNext();
    }

    /**
    * 注意这里适配器的行为: EBookInterface::getPage() 将返回两个整型,除了 BookInterface
    * 仅支持获得当前页,所以我们这里适配这个行为
    *
    * @return int
    */
    public function getPage(): int
    {
        return $this->eBook->getPage()[0];
    }
}
  • EBookInterface.php

<?php

namespace DesignPatterns\Structural\Adapter;

interface EBookInterface
{
    public function unlock();

    public function pressNext();

    /**
    * 返回当前页和总页数,像 [10, 100] 是总页数100中的第10页。
    *
    * @return int[]
    */
    public function getPage(): array;
}
  • Kindle.php

<?php

namespace DesignPatterns\Structural\Adapter;

/**
* 这里是适配过的类. 在生产代码中, 这可能是来自另一个包的类,一些供应商提供的代码。
* 注意它使用了另一种命名方案并用另一种方式实现了类似的操作
*/
class Kindle implements EBookInterface
{
    /**
    * @var int
    */
    private $page = 1;

    /**
    * @var int
    */
    private $totalPages = 100;

    public function pressNext()
    {
        $this->page++;
    }

    public function unlock()
    {
    }

    /**
    * 返回当前页和总页数,像 [10, 100] 是总页数100中的第10页。
    *
    * @return int[]
    */
    public function getPage(): array
    {
        return [$this->page, $this->totalPages];
    }
}

test

  • Tests/AdapterTest.php

<?php

namespace DesignPatterns\Structural\Adapter\Tests;

use DesignPatterns\Structural\Adapter\Book;
use DesignPatterns\Structural\Adapter\EBookAdapter;
use DesignPatterns\Structural\Adapter\Kindle;
use PHPUnit\Framework\TestCase;

class AdapterTest extends TestCase
{
    public function testCanTurnPageOnBook()
    {
        $book = new Book();
        $book->open();
        $book->turnPage();

        $this->assertEquals(2, $book->getPage());
    }

    public function testCanTurnPageOnKindleLikeInANormalBook()
    {
        $kindle = new Kindle();
        $book = new EBookAdapter($kindle);

        $book->open();
        $book->turnPage();

        $this->assertEquals(2, $book->getPage());
    }
}

The Growth Path of PHP Internet Architect * The Ultimate Guide to "Design Patterns"

PHP Internet Architect 50K Growth Guide + Industry Problem Solving Guide (Continuous Update)

Interview with 10 companies, get 9 offers, PHP interview questions in 2020

★If you like my article and want to communicate and learn with more senior developers, get more technical consultation and guidance related to interviews with big companies, welcome to join our group, password: phpzh (group number 856460874).

The latest PHP advanced tutorial in 2020, full series!

If the content is good, I hope everyone will support and encourage you to give a like/like, and welcome to communicate together; in addition, if you have any questions, you can suggest what you want to see in the comments.

Guess you like

Origin blog.csdn.net/weixin_43814458/article/details/108587556