Advertisement
Guest User

Example of ObjectPool pattern

a guest
Oct 22nd, 2014
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class PoolObject
  4. {
  5.     static public function PoolConstructor($type, ...$params)
  6.     {
  7.         return new $type(...$params);
  8.     }
  9. }
  10.  
  11. // pool server
  12. class Pool
  13. {
  14.    
  15.     private $pool = array();
  16.    
  17.     public function getObject($type, $reference, ...$params)
  18.     {
  19.         if(!isset($this->pool[$type][$reference]))
  20.             $this->pool[$type][$reference] = $type::PoolConstructor($type, ...$params);
  21.         return $this->pool[$type][$reference];
  22.     }
  23.    
  24. }
  25.  
  26. class ejemplo extends PoolObject
  27. {
  28.    
  29.     private $ejemplo = null;
  30.    
  31.     public function __construct($hola)
  32.     {
  33.         echo $hola;
  34.     }
  35. }
  36.  
  37. $pool = new Pool();
  38.  
  39. $hola = 'hola brother';
  40.  
  41. $instanceOfEjemplo = $pool->getObject('ejemplo', 1, $hola);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement