依賴倒置原則 php
緣由 1能夠減小類之間的耦合 2提升類的複用性 mysql
class Config{ public $host="localhost"; static $port="3306"; static $user="root"; static $password=""; public $charset="utf8"; public $database="test"; public $file=""; } interface Db_Adapter{ public function connect($config); public function query($query,$handle); } class Db_Adapter_Mysql implements Db_Adapter{ private $_dbLink; public function connect($config) { if($this->_dbLink=@mysql_connect($config->host.(empty($config->port)?'':':'.$config->port),$config->user,$config->password,true)){ if(@mysql_select_db($config->database,$this->_dbLink)){ if($config->charset){ mysql_query("SET NAMES '{$config->charset}'",$this->_dbLink); } return $this->_dbLink; } } /**數據庫異常**/ throw new Db_Exception(@mysql_error($this->_dbLink)); } public function query($query,$handle){ if($resource=@mysql_query($query,$handle)){ return $resource; } } } class Db_Exception extends exception{ function __toString(){ return "<div style='color:red'>Exception{$this->getCode()}:{$this->getMessage()} in File:{$this->getFile()} on line:{$this->getLine()}</div>"; } } class Db_Adapter_sqlite implements Db_Adapter{ private $_dbLink; public function connect($config){ if($this->_dbLink=sqlite_open($config->file,0666,$error)){ return $this->_dbLink; } throw new Db_Exception($error); } public function query($query,$handle){ if($resource=@sqlite_query($query,$handle)){ return $resource; } } } class sqlFactory{ public static function factory($type){ $classname='Db_adapter_'.$type; return new $classname; } } $config=new Config(); $connect=sqlFactory::factory("mysql")->connect($config); $connect=sqlFactory::factory("SQLite")->connect($config);
class cook{ public function meal(){ echo "番茄炒蛋",PHP_EOL; } public function drink(){ echo "紫菜蛋花湯",PHP_EOL; } public function ok(){ echo '完畢',PHP_EOL; } } interface Command{ public function execute(); } class MealCommand implements Command{ private $cook; public function __construct(cook $cook){ $this->cook=$cook; } public function execute(){ $this->cook->meal(); } } class DrinkCommand implements Command{ private $cook; public function __construct(cook $cook){ $this->cook=$cook; } public function execute(){ $this->cook->drink(); } } class cookControl{ private $mealcommand; private $drinkcommand; public function addCommand(Command $mealcommand,Command $drinkcommand){ $this->mealcommand=$mealcommand; $this->drinkcommand=$drinkcommand; } public function callmeal(){ $this->mealcommand->execute(); } public function calldrink(){ $this->drinkcommand->execute(); } } $control=new cookControl; $cook=new cook; $mealcommand=new MealCommand($cook); $drinkcommand=new DrinkCommand($cook); $control->addCommand($mealcommand,$drinkcommand); $control->callmeal(); $control->calldrink();
1.什麼是開放-封閉原則
一個模塊在擴展性方面應該是開放的而在更改性能方面應該是封閉的。 sql
interface process{ public function process(); } class playerencode implements process{ public function process(){ echo "encode\r\n"; } } class playeroutput implements process{ public function process(){ echo "output\r\n"; } } class playProcess{ private $message=null; public function __construct(){ } public function callback(event $event){ $this->message=$event->click(); if($this->message instanceof process){ $this->message->process(); } } } class mp4{ public function work(){ $playProcess=new playProcess(); $playProcess->callback(new event('encode')); $playProcess->callback(new event('output')); } } class event{ private $m; public function __construct($me){ $this->m=$me; } public function click(){ switch($this->m){ case 'encode': return new playerencode(); break; case 'output': return new playeroutput(); break; } } } $mp4=new mp4; $mp4->work();
2 如何遵照開放-封閉原則
1)在設計方面充分應用 「抽象」和「封裝」的思想。
2)在系統功能編程實現方面應用面向接口的編程。 數據庫
子類必須可以替換掉它們的父類型,而且出如今父類型可以出現的任何地方。 編程
上層模塊不該該依賴於下層模塊,它們共同依賴於一個抽象(父類不能依賴子類,它們都要依賴抽象類)
抽象不能依賴於具體,具體應該依賴於抽象。 性能
class message{ public $name; public $email; public $content; public function __set($name,$value){ $this->name=$value; } public function __get($name){ if(!isset($this->$name)){ $this->$name=NULL; } } } class gbookModel{ private $bookPath; private $data; public function setBookPath($bookPath){ $this->bookPath=$bookPath; } public function getBookPath(){ return $this->bookPath; } public function open(){ } public function close(){ } public function read(){ return file_get_contents($this->bookPath); } public function write($data){ $this->data=self::safe($data)->name."&".self::safe($data)->email."\r\nsaid:\r\n".self::safe($data)->content; return file_put_contents($this->bookPath,$this->data,FILE_APPEND); } public static function safe($data){ $reflect=new ReflectionObject($data); $props=$reflect->getProperties(); $messagebox=new stdClass(); foreach($props as $prop){ $ivar=$prop->getName(); $messagebox->$ivar=trim($prop->getValue($data)); } return $messagebox; } public function delete(){ file_put_contents($this->bookPath,'it\'s empty now'); } public function readByPage(){ $handle=file($this->bookPath); $count=count($handle); $page=isset($_GET['page'])? intval($_GET['page']):1; if($page<1 || $page>$count) $page=1; $pnum=9; $begin=($page-1)*$pnum; $end=($begin+$pnum)>$count?$count:$begin+$pnum; for($i=$begin;$i<$end;$i++){ echo '<strong>',$i+1,'</strong>',$handle[$i],'<br/>'; } for($i=1;$i<ceil($count/$pnum);$i++){ echo "<a href=?page=${i}>${i}</a>"; } } } class leaveModel{ public function write(gbookModel $gb,$data){ $book=$gb->getBookPath(); $gb->write($data); } } class authorControl{ public function message(leaveModel $l,gbookModel $g,message $data){ $l->write($g,$data); } public function view(gbookModel $g){ return $g->read(); } public function viewByPage(gbookModel $g){ return $g->readByPage(); } public function delete(gbookModel $g){ $g->delete(); echo self::view($g); } } $message=new message; $message->name='lilu'; $message->email='lilugirl2005@126.com'; $message->content='a crazy phper love php so much.'; $gb=new authorControl(); $pen=new leaveModel(); $book=new gbookModel(); $book->setBookPath("C:\wamp2\a.txt"); $gb->message($pen,$book,$message); //echo $gb->view($book); echo $gb->viewByPage($book); //$gb->delete($book);