您的位置:58编程 > php模板方法模式是什么 PHP 模板方法模式

php模板方法模式是什么 PHP 模板方法模式

2023-05-04 22:33 PHP设计模式

php模板方法模式是什么 PHP 模板方法模式

php模板方法模式是什么 PHP 模板方法模式

php模板方法模式是什么

目的

模板方法是一种行为设计模式。

你可能已经遇到过很多次了。它的思想是让这个抽象模板的子类【完成】算法的行为策略。

也就是【好莱坞原则】:【别打给我们,我们打给你】。这个类不是由子类调用的,而是由相反的子类调用的。怎么样?当然是抽象的。

换句话说,这是一个算法框架,非常适合框架库。用户只需要实现一个方法,超类就可以完成这项工作。

它是解耦具体类和减少复制黏贴的一种简单方法,这就是为什么你会发现它无处不在。

UML 图

Alt TemplateMethod UML Diagram

代码

Journey.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralTemplateMethod;

abstract class Journey
{
    
    private array $thingsToDo = [];

    
    final public function takeATrip()
    {
        $this->thingsToDo[] = $this->buyAFlight();
        $this->thingsToDo[] = $this->takePlane();
        $this->thingsToDo[] = $this->enjoyVacation();
        $buyGift = $this->buyGift();

        if ($buyGift !== null) {
            $this->thingsToDo[] = $buyGift;
        }

        $this->thingsToDo[] = $this->takePlane();
    }

    
    abstract protected function enjoyVacation(): string;

    
    protected function buyGift(): ?string
    {
        return null;
    }

    private function buyAFlight(): string
    {
        return "Buy a flight ticket";
    }

    private function takePlane(): string
    {
        return "Taking the plane";
    }

    
    public function getThingsToDo(): array
    {
        return $this->thingsToDo;
    }
}

BeachJourney.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralTemplateMethod;

class BeachJourney extends Journey
{
    protected function enjoyVacation(): string
    {
        return "Swimming and sun-bathing";
    }
}

CityJourney.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralTemplateMethod;

class CityJourney extends Journey
{
    protected function enjoyVacation(): string
    {
        return "Eat, drink, take photos and sleep";
    }

    protected function buyGift(): ?string
    {
        return "Buy a gift";
    }
}

测试

Tests/JourneyTest.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralTemplateMethodTests;

use DesignPatternsBehavioralTemplateMethodBeachJourney;
use DesignPatternsBehavioralTemplateMethodCityJourney;
use PHPUnitFrameworkTestCase;

class JourneyTest extends TestCase
{
    public function testCanGetOnVacationOnTheBeach()
    {
        $beachJourney = new BeachJourney();
        $beachJourney->takeATrip();

        $this->assertSame(
            ["Buy a flight ticket", "Taking the plane", "Swimming and sun-bathing", "Taking the plane"],
            $beachJourney->getThingsToDo()
        );
    }

    public function testCanGetOnAJourneyToACity()
    {
        $cityJourney = new CityJourney();
        $cityJourney->takeATrip();

        $this->assertSame(
            [
                "Buy a flight ticket",
                "Taking the plane",
                "Eat, drink, take photos and sleep",
                "Buy a gift",
                "Taking the plane"
            ],
            $cityJourney->getThingsToDo()
        );
    }
}


阅读全文
以上是58编程为你收集整理的php模板方法模式是什么 PHP 模板方法模式全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 58编程 58biancheng.com 版权所有 联系我们
桂ICP备12005667号-32 Powered by CMS