下面这个例子演示了在原型管理器中存储用户预先定义的颜色原型,客户通过原型管理器克隆颜色对象。
<?php /** * abstract Prototype * */ abstract class ColorPrototype { //Methods abstract function copy(); } /** * Concrete Prototype * */ class Color extends ColorPrototype{ //Fields private $red; private $green; private $blue; //Constructors function __construct( $red, $green, $red) { $this->red = $red; $this->green = $green; $this->blue = $red; } /** * set red * * @param unknown_type $red */ public function setRed($red) { $this->red = $red; } /** * get red * */ public function getRed(){ return $this->red; } /** *set Green * * @param $green */ public function setGreen($green) { $this->green = $green; } /** * get Green * * @return unknown */ public function getGreen() { return $this->green ; } /** *set Blue * * @param $Blue */ public function setBlue($Blue) { $this->blue = $Blue; } /** * get Blue * * @return unknown */ public function getBlue() { return $this->blue ; } /** * Enter description here... * * @return unknown */ function copy(){ return clone $this; } function display() { echo $this->red , ',', $this->green, ',', $this->blue ,'<br>'; } } /** * Enter description here... * */ class ColorManager { // Fields static $colors = array(); // Indexers public static function add($name, $value){ self::$colors[$name] = $value; } public static function getCopy($name) { return self::$colors[$name]->copy(); } } /** *Client * */ class Client { public static function Main() { //原型:白色 ColorManager::add("white", new Color( 255, 0, 0 )); //红色可以由原型白色对象得到,只是重新修改白色: r $red = ColorManager::getCopy('white'); $red->setRed(255); $red->display(); //绿色可以由原型白色对象得到,只是重新修改白色: g $green = ColorManager::getCopy('white'); $green->setGreen(255); $green->display(); //绿色可以由原型白色对象得到,只是重新修改白色: b $Blue = ColorManager::getCopy('white'); $Blue->setBlue(255); $Blue->display(); } } ini_set('display_errors', 'On'); error_reporting(E_ALL & ~ E_DEPRECATED); Client::Main(); ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。