I have a class that when passed an id becomes/proxies/mimicks/works with/wraps an instance based on that unique id, but we can pass it a new id $obj->become($id); and it will change its internal state to work with that object instance instead. What is this design pattern called?
/edit/
Below is php pseudocode… this is the essence of the pattern I keep seeing.
class ObjectPoolManager{
protected $objectPool;
protected $currentlyManagedObject=null;
public function become($id){
//... searching $objectPool for $objectSpecifiedByIdParameter ...
$this->currentlyManagedObject = $objectSpecifiedByIdParameter;
}
/*
* All calls to the manager are delegated to the managed object
*/
public __call($name,$arguments){
return call_user_func_array(array($this->currentlyManagedObject, $name),$arguments);
}
}
Often the pattern depends as much on intent as it does implementation.
In this case, State is possible if the intent of this class is to represent a set of operations whose implementation differs based on its logic state (hence the name).
Strategy is is also possible if the intent of
becomeis to explicitly switch the algorithm (similar to selecting a compression routine to use on a subsequent Compress invocation).If the intent is to pool numreous stateless (meaning object instances with no data) such that they can be shared and therefore reduce the number of objects in memory then this could be a Flyweight.
And of course, the layer of indirection represented here is a form of Proxy.
Tell us what the intent of the code is and we can all provide better feedback.