一、 单例模式


顾名思义, 单例模式就是只实例一次,通过一个接口去实现多处需要的同一类对象的需求。

例子:

    public function __construct($config = [])
{
Yii::$app = $this;
static::setInstance($this); $this->state = self::STATE_BEGIN; $this->preInit($config); $this->registerErrorHandler($config); Component::__construct($config);
}

这是yii2应用组件容器,Appliction中的构造方法,通过构造函数,给类实现单例接口,给静态变量$app注册web应用对象。

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


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

例子:

     public static function createObject($type, array $params = [])
{
if (is_string($type)) {
return static::$container->get($type, $params);
} elseif (is_array($type) && isset($type['class'])) {
$class = $type['class'];
unset($type['class']);
return static::$container->get($class, $params, $type);
} elseif (is_callable($type, true)) {
return static::$container->invoke($type, $params);
} elseif (is_array($type)) {
throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
} throw new InvalidConfigException('Unsupported configuration type: ' . gettype($type));
}

这是yii2底层的工厂化类对象接口,通过第三方代码取实现当前web应用的工厂化模式。yii2引入的php底层预定义接口类,RefectionClass映射类,通过映射取工厂化类对象。

3.、注册模式


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

例子:

     public function set($id, $definition)
{
unset($this->_components[$id]); if ($definition === null) {
unset($this->_definitions[$id]);
return;
} if (is_object($definition) || is_callable($definition, true)) {
// an object, a class name, or a PHP callable
$this->_definitions[$id] = $definition;
} elseif (is_array($definition)) {
// a configuration array
if (isset($definition['class'])) {
$this->_definitions[$id] = $definition;
} else {
throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element.");
}
} else {
throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
}
}

这是yii2中间类服务定位器,实现不同应用组件的注册。

     public function get($id, $throwException = true)
{
if (isset($this->_components[$id])) {
return $this->_components[$id];
} if (isset($this->_definitions[$id])) {
$definition = $this->_definitions[$id];
if (is_object($definition) && !$definition instanceof Closure) {
return $this->_components[$id] = $definition;
} return $this->_components[$id] = Yii::createObject($definition);
} elseif ($throwException) {
throw new InvalidConfigException("Unknown component ID: $id");
} return null;
}

这是应用组件的获取。

     /**
* Returns the database connection component.
* @return \yii\db\Connection the database connection.
*/
public function getDb()
{
return $this->get('db');
} /**
* Returns the log dispatcher component.
* @return \yii\log\Dispatcher the log dispatcher application component.
*/
public function getLog()
{
return $this->get('log');
} /**
* Returns the error handler component.
* @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component.
*/
public function getErrorHandler()
{
return $this->get('errorHandler');
} /**
* Returns the cache component.
* @return \yii\caching\CacheInterface the cache application component. Null if the component is not enabled.
*/
public function getCache()
{
return $this->get('cache', false);
} /**
* Returns the formatter component.
* @return \yii\i18n\Formatter the formatter application component.
*/
public function getFormatter()
{
return $this->get('formatter');
} /**
* Returns the request component.
* @return \yii\web\Request|\yii\console\Request the request component.
*/
public function getRequest()
{
return $this->get('request');
} /**
* Returns the response component.
* @return \yii\web\Response|\yii\console\Response the response component.
*/
public function getResponse()
{
return $this->get('response');
}

这是表现层Application。

4.、组装模式


未完待续。。。。。。

Yii2 设计模式的更多相关文章

  1. Yii2设计模式——Yii2中用到哪些设计模式?

    "Yii2设计模式"包含了两个方面的内容:1.设计模式,2.Yii2框架. <设计模式>一书虽然以JAVA语言来表达设计模式的思想,但是设计模式远不限制于某一种特定的语 ...

  2. Yii2 设计模式——Yii2 中用到哪些设计模式?

    Yii 2 设计模式“包含了两个方面的内容:1. 设计模式,2. Yii 2 框架. <设计模式>一书虽然以JAVA语言来表达设计模式的思想,但是设计模式远不限制于某一种特定的语言,而是在 ...

  3. Yii2设计模式——静态工厂模式

    应用举例 yii\db\ActiveRecord //获取 Connection 实例 public static function getDb() { return Yii::$app->ge ...

  4. Yii2设计模式——简单工厂模式

    除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成"耦合"问题. 应用举例 yii\db\mysql\Sc ...

  5. Yii2 设计模式——静态工厂模式

    应用举例 yii\db\ActiveRecord //获取 Connection 实例 public static function getDb() { return Yii::$app->ge ...

  6. Yii2 设计模式——简单工厂模式

    除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成“耦合”问题. 应用举例 yii\db\mysql\Schema 中: // ...

  7. Yii2设计模式——工厂方法模式

    应用举例 yii\db\Schema抽象类中: //获取数据表元数据 public function getTableSchema($name, $refresh = false) { if (arr ...

  8. Yii2设计模式——注册树模式

    应用举例 在Yii.php中: <?php class ServiceLocator extends Component { //保存实例化的对象,每个对象都是单例,且有唯一string类型的I ...

  9. Yii2设计模式——单例模式

    应用举例 在Yii.php中: require __DIR__ . '/BaseYii.php'; // Yii框架的帮助类,提供框架基本的功能 class Yii extends \yii\Base ...

  10. Yii2设计模式——设计模式简介

    我们首先来思考一个问题:作为工程师,我们的价值是什么? 笔者认为是--解决用户问题. 我们的任何知识和技能,如果不能解决特定的问题,那么就是无用的屠龙之术:我们的任何经验,如果不能对解决新的问题有用, ...

随机推荐

  1. (生产)vue-router:路由

    参考:https://router.vuejs.org/zh-cn/ 安装 直接下载 / CDN https://unpkg.com/vue-router/dist/vue-router.js 使用: ...

  2. Excel操作之VLOOKUP函数

    1.作用 VLOOKUP函数是Excel中的一个纵向查找函数,它与LOOKUP函数和HLOOKUP函数属于一类函数,在工作中都有广泛应用,例如可以用来核对数据,多个表格之间快速导入数据等函数功能.功能 ...

  3. Node.js-ReferenceError: _filename is not defined

    简直不要被坑得太惨!!!你能?看出来这前面是两根下划线!两根下划线!两根下划线!太尴尬了~找了半天原因居然是这个!

  4. powershell远程连接

    最近因为工作的需要看了看powershell相关的知识,个人总结了一点有关于powershell远程连接需要做的步骤,希望对别人有所帮助. 使用powershell远程连接,需要进行 设备的配置: 1 ...

  5. ring0 进程隐藏实现

    最近在学习内核编程,记录一下最近的学习笔记. 原理:将当前进程从eprocess结构的链表中删除 无法被! process 0 0 看见 #include "HideProcess.h&qu ...

  6. 安装和使用nmon监测hadoop集群性能

    nmon是一个非常易用的监测Unix/Linux系统性能的小工具,可以在一个屏幕上通过指令切换,显示几乎你想要的所有指标,并且可以自动将指标周期性的保存成 .nmon格式文件,这个工具可以与nmon_ ...

  7. 动态原型模式 js

    动态原型模式 function Person(name,age){ this.name = name; this.age = age; if(typeof this.sayName != " ...

  8. POJ-3111 K Best---二分求最大化平均值

    题目链接: https://cn.vjudge.net/problem/POJ-3111 题目大意: 卖宝救夫:Demy要卖珠宝,n件分别价值vi 重 wi,她希望保留k件使得 最大. 解题思路: # ...

  9. 引用类型(一):Object类型

    对象表示方式 1.第一种方式:使用new操作符后跟Object构造函数 var person = new Object();<br/> person.name = 'Nicholas';& ...

  10. 5、Oracle备份(oracle备份脚本配置)

    1.1 Oracle数据库备份 1.1.1 链接Oracle介质管理库 请在数据库节点上操作. [oracle@db01/usr/openv/netbackup/bin]$ ./oracle_link ...