zoukankan      html  css  js  c++  java
  • [php]php设计模式 Prototype (原型模式)

    1 <?php
    2 /**
    3 * 原型模式
    4 *
    5 * 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象
    6 *
    7 */
    8 abstractclass Prototype
    9 {
    10 private$_id=null;
    11
    12 publicfunction __construct($id)
    13 {
    14 $this->_id =$id;
    15 }
    16
    17 publicfunction getID()
    18 {
    19 return$this->_id;
    20 }
    21
    22 publicfunction __clone() // magic function
    23 {
    24 $this->_id +=1;
    25 }
    26
    27 publicfunction getClone()
    28 {
    29 returnclone$this;
    30 }
    31 }
    32
    33 class ConcretePrototype extends Prototype
    34 {
    35 }
    36
    37 //
    38 $objPrototype=new ConcretePrototype(0);
    39 $objPrototype1=clone$objPrototype;
    40 echo$objPrototype1->getID()."<br/>";
    41 $objPrototype2=$objPrototype;
    42 echo$objPrototype2->getID()."<br/>";
    43
    44 $objPrototype3=$objPrototype->getClone();
    45 echo$objPrototype3->getID()."<br/>";
  • 相关阅读:
    启动Docker容器
    Docker 删除容器
    11.18数据库认证
    10.17权限认证
    9.16角色认证
    8.13数据库认证
    6.11Realm简介
    5.8认证流程分析
    4.7固定信息认证
    20张图表达程序员的心酸
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2090439.html
Copyright © 2011-2022 走看看