範例:摩斯電碼 (解譯器模式)

Pattern: 解譯器模式

Class Diagram: 摩斯電碼


情境:讓我們試著作一個摩斯電碼機,它會將一般句子轉成摩斯電碼的表示

  • 首先是語境類別 (Context)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

namespace App\InterpreterPattern\MorseCode;

class Context
{
/**
* @var string
*/
public $text;

/**
* @param string $text
*/
public function __construct(string $text)
{
$this->text = $text;
}
}

主要是承載要解譯的詞句,
會隨著解譯進度,改變其內容。


  • 接著是表達式類別 (Expression)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

namespace App\InterpreterPattern\MorseCode\Contracts;

use App\InterpreterPattern\MorseCode\Context;

interface Expression
{
/**
* 找出要解析的字串執行,並回傳剩餘字串
*
* @param Context $context
* @return Context
*/
public function interpret(Context $context): Context;

/**
* 解析字串後,印在控制台
*
* @param string $message
*/
public function execute(string $message);
}

這邊說明一下,所謂的摩斯電碼,
是利用滴答兩種不同長短訊號的排列組合,
來表達每一個字母符號。

例如:A的表示為 (.-)。

而在此處的範例中,
同個單字的字母會用空格 ( ) 分開,
不同單字的字母則會用斜槓 (/) 分開。

例如:Good Morning的表示會是 (–. — — -.. / – — .-. -. .. -. –.)。

字母間不區分大小寫。


按照上述規則,我想區分出兩種表達式 (Expression)。

解譯字母符號的為終端表達式 (Terminal Expression)
其他情況為非終端表達式 (NonTerminal Expression)

想法是使用非終端表達式時,表示還有字需要解譯。


  • 實作非終端表達式 (NonTerminal Expression)
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
<?php

namespace App\InterpreterPattern\MorseCode;

use App\InterpreterPattern\MorseCode\Contracts\Expression;
use App\InterpreterPattern\MorseCode\Context;

class NonTerminalExpression implements Expression
{
public function interpret(Context $context): Context
{
$head = ' ';
$context->text = trim($context->text);

$this->execute($head);
return $context;
}

/**
* @param string $message
*/
public function execute(string $message)
{
echo ' / ';
}

/**
* @param string $character
* @return boolean
*/
public function isSpace($character)
{
return $character == ' ';
}
}

此處interpret()方法會將目前解譯到的詞句,去除前後空白。
execute()方法則會印出斜槓 (/)。

而isSpace()方法,會在待會的客戶端程式碼用到。


  • 實作終端表達式 (Terminal Expression)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

namespace App\InterpreterPattern\MorseCode;

use App\InterpreterPattern\MorseCode\Contracts\Expression;
use App\InterpreterPattern\MorseCode\Context;
use App\InterpreterPattern\MorseCode\Exceptions\UndefinedTextException;

class TerminalExpression implements Expression
{
protected $mapping = [
'a' => '.-',
'b' => '-...',
'c' => '-.-.',
'd' => '-..',
'e' => '.',
'f' => '..-.',
'g' => '--.',
'h' => '....',
'i' => '..',
'j' => '.---',
'k' => '-.-',
'l' => '.-..',
'm' => '--',
'n' => '-.',
'o' => '---',
'p' => '.--.',
'q' => '--.-',
'r' => '.-.',
's' => '...',
't' => '-',
'u' => '..-',
'v' => '...-',
'w' => '.--',
'x' => '-..-',
'y' => '-.--',
'z' => '--..',
'0' => '-----',
'1' => '.----',
'2' => '..---',
'3' => '...--',
'4' => '....-',
'5' => '.....',
'6' => '-....',
'7' => '--...',
'8' => '---..',
'9' => '----.',
'.' => '.-.-.-',
',' => '--..--',
'?' => '..--..',
'/' => '-..-.',
"'" => '.----.',
'!' => '-.-.--',
];


public function interpret(Context $context): Context
{
$firstSpacePos = strpos($context->text, ' ');

if ($firstSpacePos) {
$head = substr($context->text, 0, $firstSpacePos);
$context->text = substr($context->text, $firstSpacePos);
} else {
$head = $context->text;
$context->text = '';
}

$this->execute($head);
return $context;
}

/**
* @param string $message
*/
public function execute(string $message)
{
$characters = str_split($message);
$lastKey = array_key_last($characters);

foreach ($characters as $key => $character) {
$this->encode($character);

if ($key == $lastKey) {
break;
}

$this->typeSpace();
}
}

/**
* @param string $character
*/
private function encode(string $character)
{
$character = strtolower($character);

if (!array_key_exists($character, $this->mapping)) {
throw new UndefinedTextException();
}

echo $this->mapping[$character];
}

private function typeSpace()
{
echo ' ';
}
}

此處interpret()方法會找出要解譯的單字,並截斷它。
execute()方法則會逐步印出單字中的每一個字母符號,彼此間以空格隔開。


  • 實作客戶端的程式碼
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
48
49
<?php

namespace App\InterpreterPattern\MorseCode;

use App\InterpreterPattern\MorseCode\NonTerminalExpression;
use App\InterpreterPattern\MorseCode\TerminalExpression;
use App\InterpreterPattern\MorseCode\Context;

class Program
{
/**
* @var TerminalExpression
*/
protected $terminalExpression;

/**
* @var NonTerminalExpression
*/
protected $nonTerminalExpression;

public function __construct()
{
$this->terminalExpression = new TerminalExpression();
$this->nonTerminalExpression = new NonTerminalExpression();
}

/**
* @param string $text
*/
public function encode(string $text)
{
try {
$context = new Context(trim($text));

while (strlen($context->text) > 0) {
$firstCharacter = substr($context->text, 0, 1);

if ($this->nonTerminalExpression->isSpace($firstCharacter)) {
$context = $this->nonTerminalExpression->interpret($context);
continue;
}

$context = $this->terminalExpression->interpret($context);
}
} catch (\Throwable $th) {
throw $th;
}
}
}

最後讓我們來看客戶端程式碼怎麼跑吧!

以Hello World為例:

  1. 首先會將Hello World轉成語境類別 (Context)
  2. 終端表達式 (Terminal Expression) 會截取出Hello這個單字,印出它的摩斯電碼。
  3. 非終端表達式 (NonTerminal Expression) 則會去除空白,印出斜槓 (/)。
  4. 終端表達式 (Terminal Expression) 會截取出World這個單字,印出它的摩斯電碼。
  5. 客戶端程式碼判斷解譯完成,結束迴圈。

[單一職責原則]
語境類別 (Context) :負責乘載要解譯的詞句。
非終端表達式 (NonTerminal Expression) :負責連結解譯單字間的文法。
終端表達式 (Terminal Expression) :負責解譯每一個字母符號。

[開放封閉原則]
增加要轉譯的字母符號時,僅需修改終端表達式 (Terminal Expression)。

[依賴反轉原則]
透過表達式 (Expression) 接口,
確保各個表達式都有interpret()方法與execute()方法。


現實中幾乎沒有機會使用到的設計模式,
範例想了很多天,希望這樣有傳達出這個模式的精神!

另外這個範例還沒有完成decode()方法,
也就是從摩斯電碼轉回一般句子。

之後有時間會試著實作看看。

ʕ •ᴥ•ʔ:目前心目中前三難的設計模式。