单例模式之继承碰见的问题
来源:互联网 发布:怎么做一个淘宝客 编辑:程序博客网 时间:2024/06/05 18:33
<?php// 单例模式之继承class Singleton{protected static $ins = null;private final function __construct() { }protected final function __clone() { }// public static function getIns() {// if(self::$ins === null){// self::$ins = new self();// }// return self::$ins;// }public static function getIns() {if(static::$ins === null){static::$ins = new static();}return static::$ins;}}class Child extends Singleton{// protected static $ins = null;}/*输出结果为:bool(true) object(Singleton)#1 (0) { }问题:对象 $c1, $c2 竟然都是 Singleton 的实例 ???解决方法:将 getIns() 方法中关键字 self 替换为 static, 利用后期静态绑定的特性*/$c1 = Child::getIns();$c2 = Child::getIns();var_dump($c1 === $c2); //truevar_dump($c1);// ------------------------------------------------------------------------// 另一个问题/*输出结果为:bool(true) object(Child)#1 (0) { }问题:对象 $c3 竟然是 Child 的实例, 实际上应该是 Singleton 的实例 ???原因:因为 $ins 属性是从父类 Singleton 继承过来的, 当第一次调用 Child::getIns() 时, $ins = new Child() 当再次调用 Singleton::getIns() 时, $ins 已经被实例过了, 而且指向 Child 的实例, 所以此时 $c3 变成了 Child 的实例解决方法:在 Child 类中, 声明自己独有的 $ins 属性*/$c3 = Singleton::getIns();var_dump($c1 === $c3);var_dump($c3);
后期静态绑定的 getIns() 方法还会有一个问题:
若 Singleton 的 $ins 属性被设置为 private 的,子类 Child 必须设置自己的 $ins 属性,
因为 static::$ins 优先寻找子类自己的 $ins 属性,但由于子类没有声明且父类的不能继承,此时调用 Child::getIns()
方法便会报错:
Fatal error: Cannot access property Child::$ins in D:\wamp\www\mycode\DesignPattern\Singleton.php on line 27
解决方法:
将父类 Singleton 的 $ins 属性设置为 protected 的 或者 设置子类 Child 自己的 $ins 属性
0 0
- 单例模式之继承碰见的问题
- 碰见的问题
- 刚碰见的问题
- 工作当中碰见的问题
- 单例继承模式
- 单例模式之懒汉的并发问题
- 单例模式之懒汉的并发问题
- java之单例设计模式,继承,final关键字总结
- java自学之路-----继承 单例设计模式
- 单例模式的问题
- 碰见C语言的几个小问题
- 网站制作中碰见的一些问题。
- 在使用lucene时碰见的问题
- android碰见的问题(1)
- 数据迁移中碰见的一些问题
- 设计模式之———单例模式的问题及效率比较
- 设计模式之单例模式二(解决无序写入的问题)
- C++单件模式下的继承
- 谁先倒(模拟)
- TexturePacker PhysicsEditor
- 利用github创建外网svn服务器
- 消息队列RabbitMQ与Spring集成
- Opengl函数(4)
- 单例模式之继承碰见的问题
- session Bean是什么?
- 【C语言简单说】十八:二维数组
- JPA学习(五):java持久化查询语言JPQL--动态查询的使用及语法限制
- cblas安装以及使用实例
- matlab Yalmip 求解电力系统鲁棒调度问题例子
- assets目录下的zip文件拷贝到sd卡下
- 页面置换算法--LFU算法实现-O(1)时间复杂度
- HPUOJ---2017寒假作业--专题-1/G-美素数