Created
December 16, 2012 02:40
-
-
Save jmather/4302634 to your computer and use it in GitHub Desktop.
Proxy Pattern Example
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 | |
interface MyAPI | |
{ | |
public function getThing(); | |
} | |
class MyApiImplentation implements MyAPI | |
{ | |
public function getThing() | |
{ | |
return 'some remote call'; | |
} | |
} | |
class MyApiCache implements MyAPI | |
{ | |
private $cache; | |
private $api; | |
public function __construct($cache, MyAPI $api) | |
{ | |
$this->cache = $cache; | |
$this->api = $api; | |
} | |
public function getThing() | |
{ | |
if ($cache->hasItem() == false) | |
{ | |
$info = $this->api->getThing(); | |
$this->cache->store($info); | |
} | |
return $this->cache->getItem(); | |
} | |
} | |
$api = new MyAPIImplementation(); | |
$cache = new SomeCacheSystem(); | |
$real_api_to_use = new MyApiCache($cache, $api); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jmather.com/2012/12/15/about-the-proxy-pattern/