Last active
August 26, 2023 20:30
-
-
Save mcsee/7b6a2b5eb49cbb4dc690a2657d6837d0 to your computer and use it in GitHub Desktop.
This file contains 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
<? | |
final class Book { | |
// Just Book related Stuff | |
} | |
interface BookRetriever { | |
public function bookByTitle(string $title); | |
} | |
final class DatabaseLibrarian implements BookRetriever { | |
public function bookByTitle(string $title) { | |
// Go to the database (not global hopefully) | |
} | |
} | |
final class HotSpotLibrarian implements BookRetriever { | |
// You always look for real life metaphors | |
private $inbox; | |
private $realRetriever; | |
public function bookByTitle(string $title) { | |
if ($this->inbox->includesTitle($title)) { | |
// You are lucky. Someone has just returned the book copy. | |
return $this->inbox->retrieveAndRemove($title); | |
} else { | |
return $this->realRetriever->bookByTitle($title); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment