设计模式---原型模式Prototype(创建型)

1. 概述

  当对象的构造函数非常复杂,在生成新对象的时候非常耗时间和资源的情况下,通过复制一个指定类型的对象来创建更多同类型的对象。这个指定的对象可被称为“原型”对象。原型模式的主要思想是基于现有的对象克隆一个新的对象,一般是由对象的内部提供克隆的方法,通过该方法返回一个对象的副本。

  浅拷贝和深拷贝:浅拷贝是简单克隆体,对应对象内部的引用对象,只是克隆地址;深拷贝是克隆一个完全一模一样的对象,引用对象指向不同的内在地址。

2. 示例

<?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();  
?>  

 

 

posted @ 2014-06-09 16:22  飛雲若雪  阅读(438)  评论(0编辑  收藏  举报