Proxy

Intent
Provide a surrogate or placeholder for another object to control access to it
Motivation
We want to provide a wrapper around a set of classes to unify their access protocols
Implementation
  • Provide a wrap method
  • intercept calls to the wraper object and forward them accordingly, may change the protocol
Example
class Fluenter { private $obj; function __construct($obj) { $this->obj = $obj; } static function MakeFluent($obj) { if ($obj instanceof fluent) return $obj; else return new fluenter($obj); } function __call($method, $args) { $result = call_user_func_array(array($this->obj, $method), $args); if (is_null($result)) return $this; else if (is_object($result) and ($result instanceof $fluent)) return $result; else throw new RuntimeException( "Fluent::__call called method $method ". "and expected a null return or a non-fluent object, ". "got (".gettype($return).") $return instead."); } }
notes