iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 16
0

Banner

流暢介面 Fluent Interface

流暢介面,常用於撰寫如同文章般容易閱讀的程式碼,如果以大頭菜來講,那麼在建立大頭菜的同時,希望可以順便賦予其鈴錢價格、數量,並且最終獲得的依舊是大頭菜物件。

UML

UML

實作

這次只需要實作一個大頭菜物件,其精隨在於賦予鈴錢價格、數量時,最後會將整個物件再回傳出來,透過被呼叫的方法來回傳當前物件。

Turnips.php

/**
 * Class Turnips.
 */
class Turnips
{
    /**
     * @var int
     */
    protected $price = 0;

    /**
     * @var int
     */
    protected $count = 0;

    /**
     * @param int $price
     * 
     * @return Turnips
     */
    public function price(int $price): Turnips
    {
        $this->price = $price;

        return $this;
    }

    /**
     * @param int $count
     * 
     * @return Turnips
     */
    public function count(int $count): Turnips
    {
        $this->count = $count;

        return $this;
    }

    /**
     * @return int
     */
    public function calculatePrice(): int
    {
        return $this->price * $this->count;
    }
}

測試

我們要來實際測試大頭菜是否能夠以流暢介面的方式來建立物件,並且計算鈴錢總價。

FluentInterfaceTest.php

/**
 * Class FluentInterfaceTest.
 */
class FluentInterfaceTest extends TestCase
{
    /**
     * @test
     */
    public function test_build_turnips()
    {
        $turnips = (new Turnips())
            ->price(100)
            ->count(40);

        $this->assertEquals(4000, $turnips->calculatePrice());
    }
}

最後測試的執行結果會獲得如下:

PHPUnit Pretty Result Printer 0.28.0 by Codedungeon and contributors.
==> Configuration: ~/php-design-pattern/vendor/codedungeon/phpunit-result-printer/src/phpunit-printer.yml

PHPUnit 9.2.6 by Sebastian Bergmann and contributors.


 ==> AbstractFactoryTest        ✔  ✔  ✔  ✔  
 ==> BuilderPatternTest         ✔  ✔  ✔  ✔  
 ==> FactoryMethodTest          ✔  ✔  ✔  ✔  
 ==> PoolPatternTest            ✔  ✔  
 ==> PrototypePatternTest       ✔  ✔  
 ==> SimpleFactoryTest          ✔  ✔  ✔  ✔  
 ==> SingletonPatternTest       ✔  
 ==> StaticFactoryTest          ✔  ✔  ✔  ✔  ✔  
 ==> AdapterPatternTest         ✔  ✔  
 ==> BridgePatternTest          ✔  ✔  ✔  
 ==> CompositePatternTest       ✔  ✔  ✔  
 ==> DataMapperTest             ✔  ✔  
 ==> DecoratorPatternTest       ✔  ✔  
 ==> DependencyInjectionTest    ✔  ✔  ✔  
 ==> FacadePatternTest          ✔  
 ==> FluentInterfaceTest        ✔  

Time: 00:00.079, Memory: 6.00 MB

OK (43 tests, 94 assertions)

完整程式碼

設計模式不難,找回快樂而已,以大頭菜為例。

參考文獻


上一篇
【PHP 設計模式大頭菜】外觀模式 Facade Pattern
下一篇
【PHP 設計模式大頭菜】享元模式 Flyweight Pattern
系列文
設計模式不難,找回快樂而已,以大頭菜為例。30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言