Yii2 设计模式
一、 单例模式
顾名思义, 单例模式就是只实例一次,通过一个接口去实现多处需要的同一类对象的需求。
例子:
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 设计模式的更多相关文章
- Yii2设计模式——Yii2中用到哪些设计模式?
"Yii2设计模式"包含了两个方面的内容:1.设计模式,2.Yii2框架. <设计模式>一书虽然以JAVA语言来表达设计模式的思想,但是设计模式远不限制于某一种特定的语 ...
- Yii2 设计模式——Yii2 中用到哪些设计模式?
Yii 2 设计模式“包含了两个方面的内容:1. 设计模式,2. Yii 2 框架. <设计模式>一书虽然以JAVA语言来表达设计模式的思想,但是设计模式远不限制于某一种特定的语言,而是在 ...
- Yii2设计模式——静态工厂模式
应用举例 yii\db\ActiveRecord //获取 Connection 实例 public static function getDb() { return Yii::$app->ge ...
- Yii2设计模式——简单工厂模式
除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成"耦合"问题. 应用举例 yii\db\mysql\Sc ...
- Yii2 设计模式——静态工厂模式
应用举例 yii\db\ActiveRecord //获取 Connection 实例 public static function getDb() { return Yii::$app->ge ...
- Yii2 设计模式——简单工厂模式
除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成“耦合”问题. 应用举例 yii\db\mysql\Schema 中: // ...
- Yii2设计模式——工厂方法模式
应用举例 yii\db\Schema抽象类中: //获取数据表元数据 public function getTableSchema($name, $refresh = false) { if (arr ...
- Yii2设计模式——注册树模式
应用举例 在Yii.php中: <?php class ServiceLocator extends Component { //保存实例化的对象,每个对象都是单例,且有唯一string类型的I ...
- Yii2设计模式——单例模式
应用举例 在Yii.php中: require __DIR__ . '/BaseYii.php'; // Yii框架的帮助类,提供框架基本的功能 class Yii extends \yii\Base ...
- Yii2设计模式——设计模式简介
我们首先来思考一个问题:作为工程师,我们的价值是什么? 笔者认为是--解决用户问题. 我们的任何知识和技能,如果不能解决特定的问题,那么就是无用的屠龙之术:我们的任何经验,如果不能对解决新的问题有用, ...
随机推荐
- C#继承 多态
1.继承 允许我们根据一个类来定义另一个类.已有的类被称为的基类(父类),新的类被称为派生类(子类). 单一继承:只能有一个基类,一个基类可以派生出多个派生类,一个类别只可以继承自一个父类. 多重继承 ...
- 软件测试技术第三次作业——打印质数printPrimes()
作业一.Use the following method printPrimes() for questions a–d. printPrimes: /** ********************* ...
- Activity的Theme主题风格
在AndroidManifest.xml文件里面: <activity name="test" android:theme="@andr ...
- Android studio 安装与配置【Android学习入门】
终于下定决心认真学习Android开发了. 之前在很多平台看到很多大牛们学习Android的经验和心得,纸上得来终觉浅. 这里推荐stormzhang老师总结的Android学习之路. 为了防止电脑卡 ...
- Eclipse+ADT+Android SDK 搭建安卓开发环境(转)
要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); eclipse-jee-luna-SR2-win32(32位);ADT-23.0.4 下载地址 环境下载 最近 ...
- msql 综合练习
8.统计列印各科成绩,各分数段人数: 课程ID,课程名称,[100-85],[85-70],[70-60],[<60] 尽管表面看上去不那么容易,其实用 CASE 可以很容易地实现: SELE ...
- Java字体优化
需求背景 最近在做的项目显示的字体感觉太丑,于是乎想着DIY改进一下. 查阅资料,总觉得别人写的都不咋地,于是决心写一篇略微完善点的关于项目字体优化方面的文章. 当然,这篇文章不会教你如何使用True ...
- Extjs4几个小知识点
1.Why user "var me=this" in Extjs4?有个英文解释很好: Say you have a method in your object A which ...
- python3绘图示例2(基于matplotlib:柱状图、分布图、三角图等)
#!/usr/bin/env python# -*- coding:utf-8 -*- from matplotlib import pyplot as pltimport numpy as npim ...
- Java项目性能瓶颈分析及定位(八)——Java线程堆栈分析(五)
对于CPU而言,常见的瓶颈主要有两种:服务器的压力很小,但是CPU的利用率却很高,这样的性能瓶颈相对比较容易定位(好比我只是说了你一句,你就哭了,你的弱点立马就暴露出来了):给服务器施加的压力很大,但 ...