通过注册树(注册器)模式可以将对象实例注册(Register::set())到一棵全局的对象树($objects)上,需要的时候从对象树上采取(Register::get())的设计方法。在工厂模式中实现了类的统一实例化,单例模式中实现了对唯一实例存在就获取不存在就实例化,而注册树模式更像是综合上面的两种方式,对类进行了统一化管理。也避免了类过多实例化的让费。
二、注册树模式的应用 tp6容器中的注册树模式
class Container implements ContainerInterface, ArrayAccess, IteratorAggregate, Countable { protected static $instance; protected $instances = []; protected $bind = []; protected $invokeCallback = []; // ... public function get($abstract) { if ($this->has($abstract)) { return $this->make($abstract); } throw new ClassNotFoundException('class not exists: ' . $abstract, $abstract); } public function instance(string $abstract, $instance) { $abstract = $this->getAlias($abstract); $this->instances[$abstract] = $instance; return $this; } public function delete($name) { $name = $this->getAlias($name); if (isset($this->instances[$name])) { unset($this->instances[$name]); } } // ... }



