範例:Line群組通知(觀察者模式)

Pattern: 觀察者模式

Class Diagram: Line群組通知


情境:讓我們用Line群組,來實作觀察者模式

  • 首先實作抽象的觀察者類別 (Observer)

其中會有接收到主題類別通知時的更新方法 (具體實作內容由子類決定)。

1
2
3
4
5
6
7
8
<?php

namespace App\ObserverPattern\LineGroup;

abstract class Observer
{
abstract public function update();
}
  • 接著實作抽象的主題類別 (Subject)

它會有新增觀察者移除觀察者通知觀察者的方法。

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\ObserverPattern\LineGroup;

use App\ObserverPattern\LineGroup\Observer;

abstract class Subject
{
/**
* @var array
*/
protected $observers = [];

public function attachObserver(Observer $observer)
{
$this->observers[] = $observer;
}

public function detachObserver(Observer $observer)
{
$index = array_search($observer, $this->observers);

if ($index >= 0) {
unset($this->observers[$index]);
}
}

public function notifyObservers()
{
foreach ($this->observers as $observer) {
$observer->update();
}
}
}

  • 利用剛剛建立的觀察者類別,實作使用者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace App\ObserverPattern\LineGroup;

use App\ObserverPattern\LineGroup\Observer;

class User extends Observer
{
protected $name;

public function __construct($name)
{
$this->name = $name;
}

public function update()
{
//使用者手機跳出通知
//使用者電腦跳出通知
}
}

  • 實作群組聊天室,並模擬使用情況
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
<?php

namespace App\ObserverPattern\LineGroup;

use App\ObserverPattern\LineGroup\Subject;
use App\ObserverPattern\LineGroup\User;

class Program extends Subject
{
protected $state = 'nothing new';

public function run()
{
//Bear加入群組
$bear = new User('Bear');
$this->attachObserver($bear);

//通知群組
$this->notifyObservers();

//Jane加入群組
$jane = new User('Jane');
$this->attachObserver($jane);

//通知群組
$this->notifyObservers();

//有新訊息,通知群組
$this->state = 'new message';
$this->notifyObservers();
$this->state = 'nothing new';

//Jane離開群組
$this->detachObserver($jane);

//通知群組
$this->notifyObservers();
}
}


[單一職責原則]
我們將主題類別 (Subject)觀察者類別 (Observer) 視作兩種不同的職責。
目前範例尚未將聊天室的職責通知的職責分離。

[開放封閉原則]
當新增新的主題類別時,我們可以輕易地新增、移除、通知對應觀察者。
亦可以新增新的觀察者類別,再將其加入既有的主題類別。

[裡氏替換原則]
透過子類來定義觀察者類別中更新方法具體的實作。
目前範例的缺點是,觀察者類別不好對更新方法取更適合的命名

[依賴反轉原則]
抽象主題類別依賴於抽象的觀察者類別。
使用者類別實作抽象的觀察者類別。

主題類別不必知道具體觀察者想做什麼,
僅需知道觀察者類別有接口可以通知更新。


ʕ •ᴥ•ʔ:架構容易,細節仍需琢磨的模式。