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应用对象。

2、 工厂模式(策略模式)

 


 

顾名思义,工厂模式就是像工厂的机器化一样取构造当前web应用所需的类对象。

例子:

 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映射类,通过映射取工厂化类对象。

3.、注册模式

 


 

顾名思义,注册模式则是通过一基类接口给基类的一个全局属性,添加不同的组件对象。

例子:

 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.、组装模式

 


 

未完待续。。。。。。

 

posted @ 2018-06-21 20:46  clevstn  阅读(1017)  评论(0编辑  收藏  举报