Yii2 設計模式

1、 單例模式php

 


 

顧名思義, 單例模式就是隻實例一次,經過一個接口去實現多處須要的同一類對象的需求。web

例子:yii2

 1    public function __construct($config = [])  2  {  3         Yii::$app = $this;  4         static::setInstance($this);  5 
 6         $this->state = self::STATE_BEGIN;  7 
 8         $this->preInit($config);  9 
10         $this->registerErrorHandler($config); 11 
12         Component::__construct($config); 13     }

這是yii2應用組件容器,Appliction中的構造方法,經過構造函數,給類實現單例接口,給靜態變量$app註冊web應用對象。app

二、 工廠模式(策略模式)yii

 


 

顧名思義,工廠模式就是像工廠的機器化同樣取構造當前web應用所需的類對象。函數

例子:this

 1     public static function createObject($type, array $params = [])  2  {  3         if (is_string($type)) {  4             return static::$container->get($type, $params);  5         } elseif (is_array($type) && isset($type['class'])) {  6             $class = $type['class'];  7             unset($type['class']);  8             return static::$container->get($class, $params, $type);  9         } elseif (is_callable($type, true)) { 10             return static::$container->invoke($type, $params); 11         } elseif (is_array($type)) { 12             throw new InvalidConfigException('Object configuration must be an array containing a "class" element.'); 13  } 14 
15         throw new InvalidConfigException('Unsupported configuration type: ' . gettype($type)); 16     }

這是yii2底層的工廠化類對象接口,經過第三方代碼取實現當前web應用的工廠化模式。yii2引入的php底層預約義接口類,RefectionClass映射類,經過映射取工廠化類對象。spa

3.、註冊模式code

 


 

顧名思義,註冊模式則是經過一基類接口給基類的一個全局屬性,添加不一樣的組件對象。component

例子:

 1     public function set($id, $definition)  2  {  3         unset($this->_components[$id]);  4 
 5         if ($definition === null) {  6             unset($this->_definitions[$id]);  7             return;  8  }  9 
10         if (is_object($definition) || is_callable($definition, true)) { 11             // an object, a class name, or a PHP callable
12             $this->_definitions[$id] = $definition; 13         } elseif (is_array($definition)) { 14             // a configuration array
15             if (isset($definition['class'])) { 16                 $this->_definitions[$id] = $definition; 17             } else { 18                 throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element."); 19  } 20         } else { 21             throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition)); 22  } 23     }

這是yii2中間類服務定位器,實現不一樣應用組件的註冊。

 1     public function get($id, $throwException = true)  2  {  3         if (isset($this->_components[$id])) {  4             return $this->_components[$id];  5  }  6 
 7         if (isset($this->_definitions[$id])) {  8             $definition = $this->_definitions[$id];  9             if (is_object($definition) && !$definition instanceof Closure) { 10                 return $this->_components[$id] = $definition; 11  } 12 
13             return $this->_components[$id] = Yii::createObject($definition); 14         } elseif ($throwException) { 15             throw new InvalidConfigException("Unknown component ID: $id"); 16  } 17 
18         return null; 19     }

這是應用組件的獲取。

 1     /**  2  * Returns the database connection component.  3  * @return \yii\db\Connection the database connection.  4      */
 5     public function getDb()  6  {  7         return $this->get('db');  8  }  9 
10     /** 11  * Returns the log dispatcher component. 12  * @return \yii\log\Dispatcher the log dispatcher application component. 13      */
14     public function getLog() 15  { 16         return $this->get('log'); 17  } 18 
19     /** 20  * Returns the error handler component. 21  * @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component. 22      */
23     public function getErrorHandler() 24  { 25         return $this->get('errorHandler'); 26  } 27 
28     /** 29  * Returns the cache component. 30  * @return \yii\caching\CacheInterface the cache application component. Null if the component is not enabled. 31      */
32     public function getCache() 33  { 34         return $this->get('cache', false); 35  } 36 
37     /** 38  * Returns the formatter component. 39  * @return \yii\i18n\Formatter the formatter application component. 40      */
41     public function getFormatter() 42  { 43         return $this->get('formatter'); 44  } 45 
46     /** 47  * Returns the request component. 48  * @return \yii\web\Request|\yii\console\Request the request component. 49      */
50     public function getRequest() 51  { 52         return $this->get('request'); 53  } 54 
55     /** 56  * Returns the response component. 57  * @return \yii\web\Response|\yii\console\Response the response component. 58      */
59     public function getResponse() 60  { 61         return $this->get('response'); 62     }

這是表現層Application。

4.、組裝模式

 


 

未完待續。。。。。。

相關文章
相關標籤/搜索