ホームページ  >  記事  >  バックエンド開発  >  ある記事では、PHP で責任連鎖設計パターンを実装する方法を詳しく説明しています (コード例付き)。

ある記事では、PHP で責任連鎖設計パターンを実装する方法を詳しく説明しています (コード例付き)。

藏色散人
藏色散人転載
2023-01-17 11:49:411879ブラウズ

この記事では、PHP デザイン パターンに関する関連知識をお届けします。主に、PHP が責任連鎖デザイン パターンを実装する方法を紹介します。一緒に見てみましょう。困っている友人に役立つことを願っています。

PHP は責任連鎖デザイン パターンを実装します

参考記事アドレス: デザイン パターンの「責任連鎖パターン」について詳しく話しましょうツール (Go 実装プロセス付き)

実装原理については参考記事を読んでください。原文は Go 言語で実装されています。PHP バージョンの実装とフレームワークは次のとおりです。ハイパーフを使用します。

ファイル構造:

IndexController が呼び出し側、UserInfoEntity ユーザー エンティティはユーザー情報を格納するために使用され、フローにはさまざまな処理プロセスが含まれます

ある記事では、PHP で責任連鎖設計パターンを実装する方法を詳しく説明しています (コード例付き)。

##IndexController

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;
use App\Service\Entity\UserInfoEntity;
use App\Service\Flow\Cashier;
use App\Service\Flow\Clinic;
use App\Service\Flow\Pharmacy;
use App\Service\Flow\Reception;
use App\Service\StartHandler;
class IndexController extends AbstractController
{
    public function index()
    {
        $startHandler = new StartHandler();
        $userInfo = (new UserInfoEntity())->setName(&#39;zhangsan&#39;);
        $startHandler->setNextHandler(new Reception())
            ->setNextHandler(new Clinic())
            ->setNextHandler(new Cashier())
            ->setNextHandler(new Pharmacy());
        $startHandler->execute($userInfo);
    }
}

UserInfoEntity

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Entity;
class UserInfoEntity
{
    private string $name;
    private bool $registrationDone = false;
    private bool $doctorCheckUpDone = false;
    private bool $medicineDone = false;
    private bool $paymentDone = false;
    public function getName(): string
    {
        return $this->name;
    }
    public function setName(string $name): UserInfoEntity
    {
        $this->name = $name;
        return $this;
    }
    public function isRegistrationDone(): bool
    {
        return $this->registrationDone;
    }
    public function setRegistrationDone(bool $registrationDone): UserInfoEntity
    {
        $this->registrationDone = $registrationDone;
        return $this;
    }
    public function isDoctorCheckUpDone(): bool
    {
        return $this->doctorCheckUpDone;
    }
    public function setDoctorCheckUpDone(bool $doctorCheckUpDone): UserInfoEntity
    {
        $this->doctorCheckUpDone = $doctorCheckUpDone;
        return $this;
    }
    public function isMedicineDone(): bool
    {
        return $this->medicineDone;
    }
    public function setMedicineDone(bool $medicineDone): UserInfoEntity
    {
        $this->medicineDone = $medicineDone;
        return $this;
    }
    public function isPaymentDone(): bool
    {
        return $this->paymentDone;
    }
    public function setPaymentDone(bool $paymentDone): UserInfoEntity
    {
        $this->paymentDone = $paymentDone;
        return $this;
    }
}

HandlerInterface

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;
use App\Service\Entity\UserInfoEntity;
interface HandlerInterface
{
    public function setNextHandler(HandlerInterface $handler): HandlerInterface;
    public function execute(UserInfoEntity $info);
    public function do(UserInfoEntity $info);
}

AbstractHandler

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;
use App\Service\Entity\UserInfoEntity;
class AbstractHandler implements HandlerInterface
{
    private HandlerInterface $nextHandler;
    public function setNextHandler(HandlerInterface $handler): HandlerInterface
    {
        $this->nextHandler = $handler;
        return $this->nextHandler;
    }
    public function execute(UserInfoEntity $info)
    {
        if (! empty($this->nextHandler)) {
            try {
                $this->nextHandler->do($info);
            } catch (\Exception $e) {
                return;
            }
            return $this->nextHandler->execute($info);
        }
    }
    public function do(UserInfoEntity $info)
    {
        // TODO: Implement do() method.
    }
}

StartHandler

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;
class StartHandler extends AbstractHandler
{
}

Cashier

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Cashier extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo &#39;收费&#39; . PHP_EOL;
        $info->setPaymentDone(true);
    }
}

クリニック

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Clinic extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo &#39;诊室&#39; . PHP_EOL;
        $info->setDoctorCheckUpDone(true);
    }
}

薬局

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Pharmacy extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo &#39;药房&#39; . PHP_EOL;
        $info->setMedicineDone(true);
    }
}

受付

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
// 挂号
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Reception extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo &#39;挂号&#39; . PHP_EOL;
        $info->setRegistrationDone(true);
    }
}

単体テストを作成し、indexController のインデックス メソッドを実行します。結果は次のとおりです。

ある記事では、PHP で責任連鎖設計パターンを実装する方法を詳しく説明しています (コード例付き)。

推奨学習: 「

PHP ビデオ チュートリアル

以上がある記事では、PHP で責任連鎖設計パターンを実装する方法を詳しく説明しています (コード例付き)。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はlearnku.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。