您好,登錄后才能下訂單哦!
本篇文章為大家展示了PHP面向?qū)ο笾械墓ぷ鲉卧鞘裁矗瑑?nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
工作單元
這個(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ù)庫中。注意它內(nèi)部調(diào)用的$obj->finder()方法是領(lǐng)域模式中通過HelperFactory工廠類生成一個(gè)相對(duì)應(yīng)的數(shù)據(jù)映射器類并return過來。
HelperFactory這個(gè)類下面沒有具體實(shí)現(xiàn)(原文也沒有實(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ì)象的小倉庫 private $dirty = array(); //存放需要在數(shù)據(jù)庫中修改的對(duì)象 private $new = array(); //存放需要在數(shù)據(jù)庫中新增的對(duì)象 private $delete = array(); //存放需要在數(shù)據(jù)庫中刪除的對(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ù)庫表調(diào)用這個(gè)類時(shí)出現(xiàn)ID重復(fù)的問題 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ù)庫交互進(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)); } //通過工廠類來實(shí)例化一個(gè)特定類型的數(shù)據(jù)映射器對(duì)象,例如VenueMapper //這個(gè)對(duì)象將被標(biāo)識(shí)映射器中的performOperations方法調(diào)用用于與數(shù)據(jù)庫交互進(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ù)庫的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ù)庫數(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){ //通過ID從數(shù)據(jù)庫中獲取一條數(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ù)庫 $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ù)庫 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(){ //返回一個(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ù)庫交互新增一條Venue數(shù)據(jù),以及二條space數(shù)據(jù)
上述內(nèi)容就是PHP面向?qū)ο笾械墓ぷ鲉卧鞘裁矗銈儗W(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。