This mode is relatively simple and easy to understand. It is often used in PHP frameworks. In some larger PHP frameworks, some commonly used class instances will be placed in the register during initialization. In fact, there is a registration In a static array in the register class, if you want to use it in the future, you can get the instance saved in the register directly based on the name. There is no need to go to the new class, getInstance or factory method again to obtain it. I feel that this mode is a bit like combining the singleton mode and the simple factory mode. After instantiation, it is saved in a static array (similar to the static variables of the singleton mode), and then the registered data can be obtained according to different parameters. Instances, compared with the simple factory model, can add instances dynamically without modifying the code, but what should be noted here is that they must be registered before they can be obtained.
There are several points to note in this mode:
<p><span>1. 首先就是要一个管理要注册的实例的类(例如:Register),我们就加他注册类吧。</span></p> <p><span>2. 这个</span>注册类必须有一个静态数组变量,采用键值数组(哈希)方式存储这些注册的实例,其中键对应实例名,值对应实例。</p> <p>3. 当然了,_set、_get、_unset(采用下划线开始仅仅是因为unset是关键字,然后为了统一,就都加上了)这些方法都不能少,用来设置、获取、和销毁注册器中的内容。</p> <p>4. 其实这个注册类可以看做是一个装载和卸载的过程,使用前进行装载,当这个实例不在使用时就可以卸载了。</p> <p>4. 这里还需要注意,不能放太多的实例进去,有些实例用一次就可以销毁掉了就不用保存到注册器中,免得浪费空间。</p> <p>5. 注册器中不同的名称是可以保存相同的实例的,因为他保存时是有名称作为索引。</p>
<?<span>php </span><span>/*</span><span>* * 注册器模式——例子 * @author 燕睿涛(luluyrt@163.com) </span><span>*/</span> <span>class</span><span> Register{ </span><span>/*</span><span>* * @var array $objects * 用来保存注册过的实例的变量 </span><span>*/</span> <span>private</span> <span>static</span> <span>$_objects</span> = <span>array</span><span>(); </span><span>/*</span><span>* * 注册一个实例到注册器中 * @param string $name 要注册的类名 * @param object $obj 要注册的类实例 * @return true </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> _set(<span>$name</span>,<span>$obj</span><span>){ </span><span>if</span>(!<span>isset</span>(self::<span>$_objects</span>[<span>$name</span><span>])){ self</span>::<span>$_objects</span>[<span>$name</span>] = <span>$obj</span><span>; } </span><span>return</span> <span>true</span><span>; } </span><span>/*</span><span>* * 从注册器中解除一个实例 * @param string $name 要注册的类名 * @return true </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> _unset(<span>$name</span><span>){ </span><span>if</span>(<span>isset</span>(self::<span>$_objects</span>[<span>$name</span><span>])){ </span><span>unset</span>(self::<span>$_objects</span>[<span>$name</span><span>]); } </span><span>return</span> <span>true</span><span>; } </span><span>/*</span><span>* * 从注册器中获取一个实例 * @param string $name 要注册的类名 * @return object/false </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> _get(<span>$name</span><span>){ </span><span>if</span>(<span>isset</span>(self::<span>$_objects</span>[<span>$name</span><span>])){ </span><span>return</span> self::<span>$_objects</span>[<span>$name</span><span>]; } </span><span>return</span> <span>false</span><span>; } } </span><span>class</span><span> Man{ </span><span>public</span> <span>function</span><span> say(){ </span><span>echo</span> "My name is Users!!"<span>; } } Register</span>::_set('man1',<span>new</span><span> Man()); Register</span>::_get('man1')->say();
send Me~