Last active
December 6, 2016 06:41
-
-
Save jaceju/8e1d54b55975eefa2e0337318eb9b92b to your computer and use it in GitHub Desktop.
ISP 原則實例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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