Skip to content

Instantly share code, notes, and snippets.

@jaceju
Last active December 6, 2016 06:41
Show Gist options
  • Save jaceju/8e1d54b55975eefa2e0337318eb9b92b to your computer and use it in GitHub Desktop.
Save jaceju/8e1d54b55975eefa2e0337318eb9b92b to your computer and use it in GitHub Desktop.
ISP 原則實例
<?php
class Mailer
{
// Mailer 只依賴在 Mailable 介面, Mailable 只有一個 getMail 方法。
public function send(Mailable $target)
{
$mail = $target->getMail();
// ...
}
}
interface Mailable
{
public function getMail();
}
class Order implements Mailable // 想寄信實作 Mailable 介面就好
{
public function getMail() { /* ... */ }
}
class Message implements Mailable // 想寄信實作 Mailable 介面就好
{
public function getMail()
{
return $this->user->mail;
}
}
<?php
class Mailer
{
// 想要重複使用 Mailer ,結果所有東西都得變成 IOrder
public function send(IOrder $order)
{
$mail = $order->getReceiverMail();
// ...
}
}
class Message implements IOrder
{
public function getReceiverMail()
{
return $this->user->mail;
}
// 不得不實作,但卻是空實作的方法
public function getOrderNumber()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment