流暢介面 Fluent Interface

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

UML

UML

實作

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

Turnips.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Class FluentInterfaceTest.
*/
class FluentInterfaceTest extends TestCase
{
/**
* @test
*/
public function test_build_turnips()
{
$turnips = (new Turnips())
->price(100)
->count(40);

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)

完整程式碼

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

參考文獻