Skip to content

Instantly share code, notes, and snippets.

@jmather
Created December 16, 2012 02:40
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 jmather/4302634 to your computer and use it in GitHub Desktop.
Save jmather/4302634 to your computer and use it in GitHub Desktop.
Proxy Pattern Example
<?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);
@cordoval
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment