Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active August 26, 2023 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsee/7b6a2b5eb49cbb4dc690a2657d6837d0 to your computer and use it in GitHub Desktop.
Save mcsee/7b6a2b5eb49cbb4dc690a2657d6837d0 to your computer and use it in GitHub Desktop.
<?
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