注册树模式
2017-01-17 18:22
197 查看
注册树模式可以集中管理对象,方便调用。下面让单例模式跟注册树模式来个小小的结合
代码:
// final禁止继承 final class Singleton { // 保存实例 private static $instance; // 防止类在外部被实例化 private function __construct() { } // 防止对象被复制 private function __clone() { } // 单例入口 public static function getInstance() { if( !(self::$instance instanceof self) ) { self::$instance = new self(); // echo "new"; // 测试代码 } // echo "get\n"; // 测试代码 return self::$instance; } // 测试方法 public function test() { echo 'test'; } } // 注册树 class Register { // 注册树 protected static $tree = array(); // 挂上注册树 public static function set($key, $value) { self::$tree[$key] = $value; } // 获取注册树内容,没有内容返回空 public static function get($key) { return isset(self::$tree[$key]) ? self::$tree[$key] : null; } // 移除注册树内容,final禁止覆盖,存在$key则删除 final public static function remove($key) { if ( array_key_exists($key, self::$tree) ) { unset(self::$tree[$key]); } } } // 使用方法 // 获取 $singleton = Register::get('singleton'); echo "1:"; print_r($singleton); // 设置 Register::set('singleton', Singleton::getInstance()); $singleton = Register::get('singleton'); echo "\n2:"; print_r($singleton); $singleton->test(); // 删除 Register::remove('singleton'); $singleton = Register::get('singleton'); echo "\n3:"; print_r($singleton);
流程图:
相关文章推荐
- IIS 7 :集成管道模式下的文件处理类型注册
- IssuVision是怎样完成观察器模式的注册和通知的
- JSP模式2 实现用户注册的功能
- 《PHP设计模式介绍》第五章 注册模式
- DAO设计模式---实现一个简单的注册(上)
- PHP设计模式 注册表模式(多个类的注册)
- JSP模式2 实现用户注册的功能
- JSP模式2 实现用户注册的功能
- JSP模式2 实现用户注册的功能
- 设计模式之我见 浅谈寄存器(注册模式)
- JSP+JAVABEAN+SERVLET模式的注册实例实现
- JSP的开发模式和设计模式(MVC)和实例---注册
- 模式2——用户注册
- JSP+JAVABEAN+SERVLET模式的注册实例实现
- 我也设计模式——6.注册工厂
- 基于服务的企业集成模式轻松入门,第 3 部分:Web services 和注册中心
- IIS由64位模式修改为32位模式,ASP.NET需要重新注册
- JSP模式2 实现用户注册的功能
- 设计模式--注册模式
- DAO设计模式---实现一个简单的注册(中)