zoukankan      html  css  js  c++  java
  • PHP设计模式笔记八:原型模式 -- Rango韩老师 http://www.imooc.com/learn/236

    原型模式

      概述:

      1、与工厂模式作用类似,都是用来创建对象

      2、与工厂模式的实现不同,原型模式是先创建好一个原型对象,然后通过clone原型对象来创建新的对象,这样就免去了类创建时重复的初始化操作

      3、原型模式适用于大对象的创建,创建一个大对象需要很大的开销,如果每次new就会消耗很大,原型模式仅需内存拷贝即可

      代码如下:

      1、编写一个画图类: 

     1 <?php
     2 namespace IMooc;
     3 
     4 class Canvas{
     5 
     6     public $data;
     7     /**
     8      * [生成一块宽为20,高为10的包含*的画布]
     9      * @param  integer $width  [画布宽]
    10      * @param  integer $height [画布高]
    11      * @return [type]          [description]
    12      */
    13     function init($width = 20, $height = 10){
    14         $data = array();
    15         for($i=0; $i<$height; $i++){
    16             for($j=0; $j<$width; $j++){
    17                 $data[$i][$j] = '*';
    18             }
    19         }
    20 
    21         $this->data = $data;
    22     }
    23     /**
    24      * [画画,也就是使用-替换*]
    25      * @param  [type] $a1 [最小高度]
    26      * @param  [type] $a2 [最大高度]
    27      * @param  [type] $b1 [最小宽度]
    28      * @param  [type] $b2 [最大宽度]
    29      * @return [type]     [description]
    30      */
    31     function rect($a1,$a2,$b1,$b2){
    32 
    33         foreach($this->data as $k1=>$line){
    34             
    35             if($k1<$a1 or $k1>$a2) continue;
    36             
    37             foreach($line as $k2=>$char){
    38                 if($k2<$b1 or $k2>$b2) continue;
    39                 //使用-替换*
    40                 $this->data[$k1][$k2] = '-';
    41             }
    42         }
    43     }
    44 
    45     /**
    46      * [输出到浏览器]
    47      * @return [type] [description]
    48      */
    49     function draw(){
    50 
    51         foreach($this->data as $line){
    52             foreach($line as $char){
    53                 echo $char;
    54             }
    55             echo "<br>";
    56         }
    57     }
    58 
    59 }

      在入口文件index.php实例化Canvas对象,进行绘图  

     1 <?php
     2 
     3 define('BASE_PATH',__DIR__);
     4 // echo BASE_PATH;
     5 require './IMooc/Framework.php';
     6 spl_autoload_register('IMoocFramework::autoload');
     7 
     8 $canvas1 = new IMoocCanvas();
     9 $canvas1->init();
    10 
    11 $canvas2 = new IMoocCanvas();
    12 $canvas2->init();
    13 
    14 $canvas1->rect(3,6,4,12);
    15 $canvas1->draw();
    16 
    17 $canvas2->rect(2,6,4,8);
    18 //$canvas2->draw();
    output输出结果
    ******************** ******************** ******************** ****---------******* ****---------******* ****---------******* ****---------******* ******************** ******************** ******************** 

     创建两个Canvas对象,如果Canvas初始化工作很多,很复杂,实例化对象对资源消耗就比较严重,这时可以考虑使用原型模式进行优化

    <?php
    
    define('BASE_PATH',__DIR__);
    // echo BASE_PATH;
    require './IMooc/Framework.php';
    spl_autoload_register('IMoocFramework::autoload');
    
    $prototype = new IMoocCanvas();
    $prototype->init();
    
    //使用clone避免每次直接进行初始化操作
    $canvas1 = clone $prototype;
    $canvas1->rect(3,6,4,12);
    $canvas1->draw();
    
    $canvas2 = clone $prototype;
    $canvas2->rect(3,6,4,12);
    $canvas2->draw();

      

      

  • 相关阅读:
    题解 P1587 【[NOI2016]循环之美】
    PKUSC2019颓废记
    使用ImageIO.write上传二维码文件时候,提示系统找不到指定路径
    rt.jar包添加源文件只需要关联到已安装对应jdk目录下source.zip源码文件即可
    Kali Linux安装中文输入法
    性能测试(一)——理发店模型
    瑜伽,不仅仅是瑜伽,敬艾扬格大师
    为什么想做测试,我的测试开端
    责任链模式-Chain of responsibility
    后缀数组-基础
  • 原文地址:https://www.cnblogs.com/helloJiu/p/6160199.html
Copyright © 2011-2022 走看看