欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP面向?qū)ο笾ぷ鲉卧?實(shí)例講解)

 更新時(shí)間:2017年06月26日 08:08:12   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇PHP面向?qū)ο笾ぷ鲉卧?實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

工作單元

這個(gè)模式涉及到了領(lǐng)域模型、數(shù)據(jù)映射器和標(biāo)識(shí)映射,這里就統(tǒng)一進(jìn)行整理和回顧了。

$venue = new \woo\domain\Venue(null,"The Green Tree");

\woo\domain\ObjectWatcher::instance()->performOperations();

現(xiàn)在以上面的二行客戶端代碼為切入點(diǎn)大概的敘述一下這個(gè)模式是怎么工作的。

第一句在使用領(lǐng)域模型對(duì)象創(chuàng)建一個(gè)對(duì)象的時(shí)候,它就調(diào)用了標(biāo)識(shí)映射ObjectWatcher類

將自己標(biāo)記為一個(gè)需要新增的對(duì)象。第二句的performOperations方法將保存在標(biāo)識(shí)映射器的屬性$new中的對(duì)象

插入到了數(shù)據(jù)庫(kù)中。注意它內(nèi)部調(diào)用的$obj->finder()方法是領(lǐng)域模式中通過(guò)HelperFactory工廠類生成一個(gè)相對(duì)應(yīng)的數(shù)據(jù)映射器類并return過(guò)來(lái)。

HelperFactory這個(gè)類下面沒(méi)有具體實(shí)現(xiàn)(原文也沒(méi)有實(shí)現(xiàn)),其實(shí)就是根據(jù)參數(shù)傳入的類的類型使用條件分支創(chuàng)建對(duì)應(yīng)的數(shù)據(jù)映射器。

下面直接看代碼和注釋進(jìn)行理解。

namespace woo\domain;

//標(biāo)識(shí)映射
class ObjectWatcher{
  
  private $all = array();        //存放對(duì)象的小倉(cāng)庫(kù)
  private $dirty = array();      //存放需要在數(shù)據(jù)庫(kù)中修改的對(duì)象
  private $new = array();        //存放需要在數(shù)據(jù)庫(kù)中新增的對(duì)象
  private $delete = array();      //存放需要在數(shù)據(jù)庫(kù)中刪除的對(duì)象
  private static $instance;      //單例
  
  
  private function __construct (){}
  
  static function instance(){
    if(!self::$instance){
      self::$instance = new ObjectWatcher();
    }
    return self::$instance;
  }
  
  //獲取一個(gè)唯一的標(biāo)識(shí),這里采用了領(lǐng)域類類名+ID的方式創(chuàng)建一個(gè)唯一標(biāo)識(shí),避免多個(gè)數(shù)據(jù)庫(kù)表調(diào)用這個(gè)類時(shí)出現(xiàn)ID重復(fù)的問(wèn)題
  function globalKey(DomainObject $obj){
    $key = get_class($obj) . "." . $obj->getId();
    return $key;
  }
  
  //添加對(duì)象
  static function add(DomainObject $obj){
    $inst = self::instance();
    $inst->all[$inst->globalKey($obj)] = $obj;
  }
  
  //獲取對(duì)象
  static function exists($classname,$id){
    $inst = self::instance();
    $key = "$classname.$id";
    if(isset($inst->all[$key]){
      return $inst->all[$key];
    }
    return null;
  }
  
  //標(biāo)記為需要?jiǎng)h除的對(duì)象
  static function addDelete(DomainObject $obj){
    $self = self::instance();
    $self->delete[$self->globalKey($obj)] = $obj;
  }
  
  //標(biāo)記為需要修改的對(duì)象
  static function addDirty(DomainObject $obj){
    $inst = self::instance();
    if(!in_array($obj,$inst->new,true)){
      $inst->dirty[$inst->globalKey($obj)] = $obj;
    }
  }
  
  //標(biāo)記為需要新增的對(duì)象
  static function addNew(DomainObject $obj){
    $inst = self::instance();
    $inst->new[] = $obj;
  }
  
  //標(biāo)記為干凈的對(duì)象
  static function addClean(DomainObject $obj){
    $self = self::instance();
    unset($self->delete[$self->globalKey($obj)]);
    unset($self->dirty[$self->globalKey($obj)]);
    $self->new = array_filter($self->new,function($a) use($obj) {return !($a === $obj);});
  }
    
  //將上述需要增刪改的對(duì)象與數(shù)據(jù)庫(kù)交互進(jìn)行處理  
  function performOperations(){
    foreach($this->dirty as $key=>$obj){
      $obj->finder()->update($obj);    //$obj->finder()獲取一個(gè)數(shù)據(jù)映射器
    }
    foreach($this->new as $key=>$obj){
      $obj->finder()->insert($obj);
    }
    $this->dirty = array();
    $this->new = array();
  }
}


//領(lǐng)域模型
abstract class DomainObject{      //抽象基類
  
  private $id = -1;
  
  function __construct ($id=null){
    if(is_null($id)){
      $this->markNew();      //初始化時(shí)即被標(biāo)記為需要新增的對(duì)象了
    } else {
      $this->id = $id;
    }  
  }
  
  //調(diào)用了標(biāo)識(shí)映射的標(biāo)記對(duì)象的方法
  function markNew(){
    ObjectWatcher::addNew($this);
  }
  
  function markDeleted(){
    ObjectWatcher::addDelete($this);
  }
  
  function markDirty(){
    ObjectWatcher::addDirty($this);
  }
  
  function markClean(){
    ObjectWatcher::addClean($this);
  }
  
  function setId($id){
    $this->id = $id;
  }
  
  function getId(){
    return $this->id;
  }
  
  
  function finder(){
    return self::getFinder(get_class($this));
  }
  
  //通過(guò)工廠類來(lái)實(shí)例化一個(gè)特定類型的數(shù)據(jù)映射器對(duì)象,例如VenueMapper
  //這個(gè)對(duì)象將被標(biāo)識(shí)映射器中的performOperations方法調(diào)用用于與數(shù)據(jù)庫(kù)交互進(jìn)行增刪改的操作
  static function getFinder($type){
    return HelperFactory::getFinder($type);
  }
  
}


class Venue extends DomainObject {
  private $name;
  private $spaces;
  
  function __construct ($id = null,$name=null){
    $this->name= $name;
    $this->spaces = self::getCollection('\\woo\\domain\\space'); 
    parent::__construct($id);
  }
  
  function setSpaces(SpaceCollection $spaces){
    $this->spaces = $spaces;
    $this->markDirty();            //標(biāo)記為需要修改的對(duì)象
  }
  
  function addSpace(Space $space){
    $this->spaces->add($space);
    $space->setVenue($this);
    $this->markDirty();            //標(biāo)記為需要修改的對(duì)象
  }
  
  function setName($name_s){
    $this->name = $name_s;
    $this->markDirty();            //標(biāo)記為需要修改的對(duì)象
  }
  
  function getName(){
    return $this->name;
  }
}


//領(lǐng)域模型
class Space extends DomainObject{
  //.........
  function setName($name_s){
    $this->name = $name_s;
    $this->markDirty();
  }
  
  function setVenue(Venue $venue){
    $this->venue = $venue;
    $this->markDirty();
  }
}


//數(shù)據(jù)映射器
abstract class Mapper{
  
  abstract static $PDO;    //操作數(shù)據(jù)庫(kù)的pdo對(duì)象
  function __construct (){
    if(!isset(self::$PDO){
      $dsn = \woo\base\ApplicationRegistry::getDSN();
      if(is_null($dsn)){
        throw new \woo\base\AppException("no dns");
      }
      self::$PDO = new \PDO($dsn);
      self::$PDO->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION);
    }
  }
  
  //獲取標(biāo)記的對(duì)象
  private function getFroMap($id){
    return \woo\domain\ObjectWatcher::exists($this->targetClass(),$id);
  }
  
  
  //新增標(biāo)記的對(duì)象
  private function addToMap(\woo\domain\DomainObject $obj){//////
    return \woo\domain\ObjectWatcher::add($obj);
  }
  
  
  //將數(shù)據(jù)庫(kù)數(shù)據(jù)映射為對(duì)象
  function createObject($array){
    $old = $this->getFromMap($array['id']);
    if($old){return $old;}
    $obj = $this->doCreateObject($array);
    $this->addToMap($obj);
    $obj->markClean();
    return $obj;
  }

  
  function find($id){                //通過(guò)ID從數(shù)據(jù)庫(kù)中獲取一條數(shù)據(jù)并創(chuàng)建為對(duì)象  
    $old = $this->getFromMap($id);        
    if($old){return $old}            
    
    $this->selectStmt()->execute(array($id));
    $array= $this->selectStmt()->fetch();
    $this->selectStmt()->closeCursor();
    if(!is_array($array)){
      return null;
    }
    if(!isset($array['id'])){
      return null;
    }
    $object = $this->createObject($array);
    $this->addToMap($object);          
    return $object;  
  }
  
  
  function insert(\woo\domain\DomainObject $obj){      //將對(duì)象數(shù)據(jù)插入數(shù)據(jù)庫(kù)
    $this->doInsert($obj);
    $this->addToMap($obj);            
  }
  
  //需要在子類中實(shí)現(xiàn)的各個(gè)抽象方法
  abstract function targetClass();                    //獲取類的類型
  abstract function update(\woo\domain\DomainObject $objet);        //修改操作
  protected abstract function doCreateObject(array $array);        //創(chuàng)建對(duì)象
  protected abstract function selectStmt();                //查詢操作
  protected abstract function doInsert(\woo\domain\DomainObject $object);  //插入操作
  
}

class VenueMapper extends Mapper {
  function __construct (){    
    parent::__construct();  
    //預(yù)處理對(duì)象
    $this->selectStmt = self::$PDO->prepare("select * from venue where id=?");
    $this->updateStmt = self::$PDO->prepare("update venue set name=?,id=? where id=?");
    $this->insertStmt = self::$PDO->prepare("insert into venue (name) values(?)");
  }
  
  protected function getCollection(array $raw){    //將Space數(shù)組轉(zhuǎn)換成對(duì)象集合
    return new SpaceCollection($raw,$this);        
  }
  
  protected function doCreateObject (array $array){  //創(chuàng)建對(duì)象
    $obj = new \woo\domain\Venue($array['id']);
    $obj->setname($array['name']);
    return $obj;
  }
  
  protected function doInsert(\woo\domain\DomainObject $object){ //將對(duì)象插入數(shù)據(jù)庫(kù)
    print 'inserting';
    debug_print_backtrace();
    $values = array($object->getName());
    $this->insertStmt->execute($values);
    $id = self::$PDO->lastInsertId();
    $object->setId($id);
  }
  
  function update(\woo\domain\DomainObject $object){    //修改數(shù)據(jù)庫(kù)數(shù)據(jù)
    print "updation\n";
    $values = array($object->getName(),$object->getId(),$object->getId());
    $this->updateStmt->execute($values);
  }
  
  function selectStmt(){          //返回一個(gè)預(yù)處理對(duì)象
    return $this->selectStmt;
  }
  
}


//客戶端
$venue = new \woo\domain\Venue(null,"The Green Tree");        //在初始化時(shí)就被標(biāo)記為新增對(duì)象了
$venue->addSpace(new \woo\domain\Space(null,"The Space Upstairs"));  //這二行addSpace方法因?yàn)関enue已經(jīng)被標(biāo)記新增所以不會(huì)再標(biāo)記為修改對(duì)象,但是space在初始化的時(shí)候會(huì)被標(biāo)記為新增對(duì)象
$venue->addSpace(new \woo\domain\Space(null,"The Bar Stage"));      
\woo\domain\ObjectWatcher::instance()->performOperations();      //與數(shù)據(jù)庫(kù)交互新增一條Venue數(shù)據(jù),以及二條space數(shù)據(jù)

以上這篇PHP面向?qū)ο笾ぷ鲉卧?實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Zend Framework頁(yè)面緩存實(shí)例

    Zend Framework頁(yè)面緩存實(shí)例

    這篇文章主要介紹了Zend Framework頁(yè)面緩存實(shí)例,使用Zend緩存對(duì)象Zend_Cache實(shí)現(xiàn),需要的朋友可以參考下
    2014-06-06
  • 在laravel框架中使用model層的方法

    在laravel框架中使用model層的方法

    今天小編就為大家分享一篇在laravel框架中使用model層的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • PHP清除緩存的幾種方法總結(jié)

    PHP清除緩存的幾種方法總結(jié)

    這篇文章主要介紹了PHP清除緩存的幾種方法總結(jié)的相關(guān)資料,希望通過(guò)本文大家能夠掌握清除緩存的方法,需要的朋友可以參考下
    2017-09-09
  • 詳解Yii2.0使用AR聯(lián)表查詢實(shí)例

    詳解Yii2.0使用AR聯(lián)表查詢實(shí)例

    這篇文章主要介紹了詳解Yii2.0使用AR聯(lián)表查詢實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • ThinkPHP之R方法實(shí)例詳解

    ThinkPHP之R方法實(shí)例詳解

    這篇文章主要介紹了ThinkPHP的R方法,需要的朋友可以參考下
    2014-06-06
  • Yaf框架封裝的MySQL數(shù)據(jù)庫(kù)操作示例

    Yaf框架封裝的MySQL數(shù)據(jù)庫(kù)操作示例

    這篇文章主要介紹了Yaf框架封裝的MySQL數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式分析了Yaf框架基于PDO操作MySQL數(shù)據(jù)庫(kù)的相關(guān)配置、連接、增刪改查、統(tǒng)計(jì)等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Laravel多條件where查詢語(yǔ)句使用詳解

    Laravel多條件where查詢語(yǔ)句使用詳解

    這篇文章主要為大家介紹了Laravel多條件where查詢語(yǔ)句使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • PHP與Ajax相結(jié)合實(shí)現(xiàn)登錄驗(yàn)證小Demo

    PHP與Ajax相結(jié)合實(shí)現(xiàn)登錄驗(yàn)證小Demo

    AJAX即“Asynchronous Javascript And XML”(異步JavaScript和XML),是指一種創(chuàng)建交互式網(wǎng)頁(yè)應(yīng)用的網(wǎng)頁(yè)開(kāi)發(fā)技術(shù)。接下來(lái)通過(guò)本文給大家分享PHP與Ajax相結(jié)合實(shí)現(xiàn)登錄驗(yàn)證小Demo,對(duì)php ajax實(shí)現(xiàn)登錄驗(yàn)證相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • PHP反射機(jī)制案例講解

    PHP反射機(jī)制案例講解

    這篇文章主要介紹了PHP反射機(jī)制案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • PHP中is_dir()函數(shù)使用指南

    PHP中is_dir()函數(shù)使用指南

    php提供內(nèi)置函數(shù)is_dir來(lái)檢查傳入的路徑參數(shù)是否為目錄,如果是目錄則返回true。也就是說(shuō)傳入的參數(shù)是文件或者不存在都為false,因此當(dāng)前參數(shù)不能說(shuō)不是文件夾就是文件。另外需要注意的是參數(shù)支持相對(duì)路徑和絕對(duì)路徑。
    2015-05-05

最新評(píng)論