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设计模式——设计模式简介
我们首先来思考一个问题:作为工程师,我们的价值是什么? 笔者认为是--解决用户问题. 我们的任何知识和技能,如果不能解决特定的问题,那么就是无用的屠龙之术:我们的任何经验,如果不能对解决新的问题有用, ...
随机推荐
- MVC中的验证码
下面是一个完整的mvc controller类 public class CodeController : Controller { private const string CODE = " ...
- DIV三列同行
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- GitHub webstorm 及 README.md 姿势
README.md 语法格式: 规范的README文件开头都写上一个标题,这被称为大标题. 标题: #一级标题 ##二级标题 ###三级标题 ####四级标题 #####五级标题 ######六级标题 ...
- 使用Ribbon Workbench来修改停用、激活按钮的权限
在实施的过程中,有时会遇到客户为了管控使用人员的操作或防止使用人员通过停用后再激活来绕开部分逻辑,需要对激活.停用按钮赋予单独的权限.但很遗憾,在Dyanmics CRM中,并没有把停用.激活按钮单独 ...
- iOS开发之Objective-c的AES256加密和解密算法的实现
原文:http://www.lidaren.com/archives/1470 高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法. 以下实现 ...
- 模拟Chrome皮肤
话不多说,先验货: (原始状态) (最大化状态) (对比图) 为自己鼓掌!!! 哈哈,捣鼓2天终于把这个搞出来了!虽然代码一团糟,但是不难理解! 要实现这个功能需要几个组件:DWM,GDI+ 在实现这 ...
- Web前端开发规范(二)
3.HTML代码规范 .html文件必须存放在项目工程约定的目录中. .html文件的命名:以模块 + 功能的结合方式来命名,比如:newsList.html. 文档类型声明:HTML4中使用< ...
- hibernate_HelloWorld
环境准备 1.下载 hibernate 3.3.2: 2.下载 hibernate 3.4.0: 3.注意阅读 hibernate compatibility matrix(hibernate 网站, ...
- eclipse中安装thymeleaf插件完成thymeleaf模板中自动代码提示功能
插件地址:https://github.com/thymeleaf/thymeleaf-extras-eclipse-plugin 页面有介绍如何使用:
- 51nod 1366 贫富差距
题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个国家有N个公民,标记为0,1,2,...,N-1,每个公民有一个存款额.已知每个公 ...