您的位置:首页 > 其它

设计模式 八

2017-06-26 00:00 10 查看
原文 https://github.com/kamranahmedse/design-patterns-for-humans

作者用简练的语句概括几个设计模式的场景和用法,比看长篇大论心情舒畅多了,简要翻译一下加深记忆
#介绍
设计模式是对某种特定问题的解决之道
#注意
设计模式不是银弹

#设计模式的类型

创造型

结构型

行为型

#行为性设计模式
结构性设计模式专注于对象之间的职责分配,信息传达

责任链模式

命令模式

迭代器模式

适配器模式

备忘录模式

观察者模式

访问者模式

策略模式

状态模式

模版方法模式

##迭代器模式

它可以让用户通过特定的接口巡访容器中的每一个元素而不用了解底层的实现。
此外,也可以实现特定目的版本的迭代器。

在PHP中,可以使用SPL(standard PHP library)实现。收音机的电台例子。

class RadioStation
{
protected $frequency;

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

public function getFrequency(): float
{
return $this->frequency;
}
}

迭代器

use Countable;
use Iterator;

class StationList implements Countable, Iterator
{
/** @var RadioStation[] $stations */
protected $stations = [];

/** @var int $counter */
protected $counter;

public function addStation(RadioStation $station)
{
$this->stations[] = $station;
}

public function removeStation(RadioStation $toRemove)
{
$toRemoveFrequency = $toRemove->getFrequency();
$this->stations = array_filter($this->stations, function (RadioStation $station) use ($toRemoveFrequency) {
return $station->getFrequency() !== $toRemoveFrequency;
});
}

public function count(): int
{
return count($this->stations);
}

public function current(): RadioStation
{
return $this->stations[$this->counter];
}

public function key()
{
return $this->counter;
}

public function next()
{
$this->counter++;
}

public function rewind()
{
$this->counter = 0;
}

public function valid(): bool
{
return isset($this->stations[$this->counter]);
}
}

使用

$stationList = new StationList();

$stationList->addStation(new RadioStation(89));
$stationList->addStation(new RadioStation(101));
$stationList->addStation(new RadioStation(102));
$stationList->addStation(new RadioStation(103.2));

foreach($stationList as $station) {
echo $station->getFrequency() . PHP_EOL;
}

$stationList->removeStation(new RadioStation(89)); // Will remove station 89

##中介者

包装了一系列对象相互作用的方式,使得这些对象不必相互明显作用,从而使它们可以松散偶合。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用,保证这些作用可以彼此独立的变化。

聊天室是中介者

interface ChatRoomMediator
{
public function showMessage(User $user, string $message);
}

// Mediator
class ChatRoom implements ChatRoomMediator
{
public function showMessage(User $user, string $message)
{
$time = date('M d, y H:i');
$sender = $user->getName();

echo $time . '[' . $sender . ']:' . $message;
}
}

使用方

class User {
protected $name;
protected $chatMediator;

public function __construct(string $name, ChatRoomMediator $chatMediator) {
$this->name = $name;
$this->chatMediator = $chatMediator;
}

public function getName() {
return $this->name;
}

public function send($message) {
$this->chatMediator->showMessage($this, $message);
}
}

$mediator = new ChatRoom();

$john = new User('John Doe', $mediator);
$jane = new User('Jane Doe', $mediator);

$john->send('Hi there!');
$jane->send('Hey!');

// Output will be
// Feb 14, 10:58 [John]: Hi there!
// Feb 14, 10:58 [Jane]: Hey!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Design Patterns