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

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

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

工作單元

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

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

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

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

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

將自己標記為一個需要新增的對象。第二句的performOperations方法將保存在標識映射器的屬性$new中的對象

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

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

下面直接看代碼和注釋進行理解。

namespace woo\domain;

//標識映射
class ObjectWatcher{
  
  private $all = array();        //存放對象的小倉庫
  private $dirty = array();      //存放需要在數(shù)據(jù)庫中修改的對象
  private $new = array();        //存放需要在數(shù)據(jù)庫中新增的對象
  private $delete = array();      //存放需要在數(shù)據(jù)庫中刪除的對象
  private static $instance;      //單例
  
  
  private function __construct (){}
  
  static function instance(){
    if(!self::$instance){
      self::$instance = new ObjectWatcher();
    }
    return self::$instance;
  }
  
  //獲取一個唯一的標識,這里采用了領(lǐng)域類類名+ID的方式創(chuàng)建一個唯一標識,避免多個數(shù)據(jù)庫表調(diào)用這個類時出現(xiàn)ID重復的問題
  function globalKey(DomainObject $obj){
    $key = get_class($obj) . "." . $obj->getId();
    return $key;
  }
  
  //添加對象
  static function add(DomainObject $obj){
    $inst = self::instance();
    $inst->all[$inst->globalKey($obj)] = $obj;
  }
  
  //獲取對象
  static function exists($classname,$id){
    $inst = self::instance();
    $key = "$classname.$id";
    if(isset($inst->all[$key]){
      return $inst->all[$key];
    }
    return null;
  }
  
  //標記為需要刪除的對象
  static function addDelete(DomainObject $obj){
    $self = self::instance();
    $self->delete[$self->globalKey($obj)] = $obj;
  }
  
  //標記為需要修改的對象
  static function addDirty(DomainObject $obj){
    $inst = self::instance();
    if(!in_array($obj,$inst->new,true)){
      $inst->dirty[$inst->globalKey($obj)] = $obj;
    }
  }
  
  //標記為需要新增的對象
  static function addNew(DomainObject $obj){
    $inst = self::instance();
    $inst->new[] = $obj;
  }
  
  //標記為干凈的對象
  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);});
  }
    
  //將上述需要增刪改的對象與數(shù)據(jù)庫交互進行處理  
  function performOperations(){
    foreach($this->dirty as $key=>$obj){
      $obj->finder()->update($obj);    //$obj->finder()獲取一個數(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();      //初始化時即被標記為需要新增的對象了
    } else {
      $this->id = $id;
    }  
  }
  
  //調(diào)用了標識映射的標記對象的方法
  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));
  }
  
  //通過工廠類來實例化一個特定類型的數(shù)據(jù)映射器對象,例如VenueMapper
  //這個對象將被標識映射器中的performOperations方法調(diào)用用于與數(shù)據(jù)庫交互進行增刪改的操作
  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();            //標記為需要修改的對象
  }
  
  function addSpace(Space $space){
    $this->spaces->add($space);
    $space->setVenue($this);
    $this->markDirty();            //標記為需要修改的對象
  }
  
  function setName($name_s){
    $this->name = $name_s;
    $this->markDirty();            //標記為需要修改的對象
  }
  
  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ù)庫的pdo對象
  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);
    }
  }
  
  //獲取標記的對象
  private function getFroMap($id){
    return \woo\domain\ObjectWatcher::exists($this->targetClass(),$id);
  }
  
  
  //新增標記的對象
  private function addToMap(\woo\domain\DomainObject $obj){//////
    return \woo\domain\ObjectWatcher::add($obj);
  }
  
  
  //將數(shù)據(jù)庫數(shù)據(jù)映射為對象
  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){                //通過ID從數(shù)據(jù)庫中獲取一條數(shù)據(jù)并創(chuàng)建為對象  
    $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){      //將對象數(shù)據(jù)插入數(shù)據(jù)庫
    $this->doInsert($obj);
    $this->addToMap($obj);            
  }
  
  //需要在子類中實現(xiàn)的各個抽象方法
  abstract function targetClass();                    //獲取類的類型
  abstract function update(\woo\domain\DomainObject $objet);        //修改操作
  protected abstract function doCreateObject(array $array);        //創(chuàng)建對象
  protected abstract function selectStmt();                //查詢操作
  protected abstract function doInsert(\woo\domain\DomainObject $object);  //插入操作
  
}

class VenueMapper extends Mapper {
  function __construct (){    
    parent::__construct();  
    //預處理對象
    $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)換成對象集合
    return new SpaceCollection($raw,$this);        
  }
  
  protected function doCreateObject (array $array){  //創(chuàng)建對象
    $obj = new \woo\domain\Venue($array['id']);
    $obj->setname($array['name']);
    return $obj;
  }
  
  protected function doInsert(\woo\domain\DomainObject $object){ //將對象插入數(shù)據(jù)庫
    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ù)庫數(shù)據(jù)
    print "updation\n";
    $values = array($object->getName(),$object->getId(),$object->getId());
    $this->updateStmt->execute($values);
  }
  
  function selectStmt(){          //返回一個預處理對象
    return $this->selectStmt;
  }
  
}


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

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

相關(guān)文章

  • Zend Framework頁面緩存實例

    Zend Framework頁面緩存實例

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

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

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

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

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

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

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

    ThinkPHP之R方法實例詳解

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

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

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

    Laravel多條件where查詢語句使用詳解

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

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

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

    PHP反射機制案例講解

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

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

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

最新評論