若转载教程,请注明出自SW-X框架官方文档!
?php/*** 设计模式之原型模式* 小黄牛*/header("Content-type: text/html; charset=utf-8");/*** 抽象 - 原型类*/abstract class Prototype{abstract function cloned(); // 克隆方法}/*** 具体化 - 原型类*/class Plane extends Prototype{public $color; // 测试的成员属性public function cloned(){# clone 关键字,是PHP5才有的,用于克隆一个实例return clone $this;}}# 测试$res1 = new Plane();$res1->color = '蓝色'; // 赋值成员属性$res2 = $res1->cloned(); // 克隆一个实例echo "res1的颜色为:{$res1->color}<br/>";echo "res2的颜色为:{$res2->color}<br/>";
浏览器输出
res1的颜色为:蓝色res2的颜色为:蓝色
原型模式
1.抽象原型,提供了一个克隆的接口2.具体的原型,实现克隆的接口
这里只是介绍一下原型模式的核心思想,其实在实际开发中直接clone即可。
