Heim  >  Artikel  >  Backend-Entwicklung  >  In einem Artikel wird ausführlich erläutert, wie man ein Chain-of-Responsibility-Designmuster in PHP implementiert (mit Codebeispielen).

In einem Artikel wird ausführlich erläutert, wie man ein Chain-of-Responsibility-Designmuster in PHP implementiert (mit Codebeispielen).

藏色散人
藏色散人nach vorne
2023-01-17 11:49:411879Durchsuche

Dieser Artikel vermittelt Ihnen relevantes Wissen über PHP-Designmuster. Er stellt hauptsächlich vor, wie PHP das Chain-of-Responsibility-Designmuster implementiert. Ich hoffe, es wird für Freunde in Not hilfreich sein.

PHP implementiert das Chain of Responsibility-Designmuster.

Adresse des Referenzartikels: Lassen Sie uns ausführlich über das Designmuster-Tool „Chain of Responsibility Pattern“ (mit Go-Implementierungsprozess) sprechen.

Lesen Sie einfach den Referenzartikel Für das Implementierungsprinzip wird die Go-Sprache verwendet. Hier ist eine PHP-Version der Implementierung.

Dateistruktur:

IndexController ist das aufrufende Ende, die Benutzerentität UserInfoEntity wird zum Speichern von Benutzerinformationen verwendet und der Fluss enthält verschiedene Verarbeitungsprozesse

<?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);
    }
}

StartHandlerIn einem Artikel wird ausführlich erläutert, wie man ein Chain-of-Responsibility-Designmuster in PHP implementiert (mit Codebeispielen).

<?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;
    }
}

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;
use App\Service\Entity\UserInfoEntity;
interface HandlerInterface
{
    public function setNextHandler(HandlerInterface $handler): HandlerInterface;
    public function execute(UserInfoEntity $info);
    public function do(UserInfoEntity $info);
}

Clinic

<?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.
    }
}

Pharmacy

<?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
{
}

Reception

<?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);
    }
}

Schreiben Sie einen Unit-Test und führen Sie die Indexmethode von indexController aus. Die Ergebnisse sind wie folgt:

Empfehlen beendete Studie: „

PHP Video Tutorial"

Das obige ist der detaillierte Inhalt vonIn einem Artikel wird ausführlich erläutert, wie man ein Chain-of-Responsibility-Designmuster in PHP implementiert (mit Codebeispielen).. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:learnku.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen