一、 单例模式


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

例子:

    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. ADO.NET的主要对象

    ADO.NET主要分为五个对象: 1)Connection对象:用来连接程序与数据库.没有利用连接对象将数据库打开,是无法从数据库中取得数据的.Close和Dispose的区别,Close之后还可以用 ...

  2. ora-12541:tns: 无监听程序解决办法

    1.首先找到 Oracle 安装文件 中 listener.ora文件与tnsnames.ora文件: 列如:路径:E:\app\当前系统的账户名\product\11.2.0\dbhome_1\NE ...

  3. Chrome浏览器正常,IE下界面却乱了

    背景:项目实战中总会遇到一些小问题,IE特别多 Chrome浏览器页面正常,IE下界面就乱了 原因分析 1.首先想到的是代码有米有问题呢?主要指的是兼容性 2.兼容性没有问题,那我们打开IE的开发工具 ...

  4. css 两大特性:继承性和层叠性

    css 有两大特性: 继承性和层叠性, 继承性 面向对象语言都会存在继承的概念,在面向对象的语言中,继承的特点:继承了父类的属性和方法.那么我们现在主要研究css,css中没有方法,所以我们仅仅继承属 ...

  5. rem与em的区别

    这两个单位都是相对元素 rem相对根元素 em相对于父级元素

  6. check_mk检测插件编写

    参考 Writing Checks (Introduction) Writing agent based checks The New Check API http://www2.steinkogle ...

  7. Linux vi 常用指令总结

    本文根据笔者,日常常用的linux下的vi指令,进行说明 一.基本操作 1.vi 文件名 进入vi 的“命令行模式”,此模式无法编辑,只能查看 需要按下键盘的“i”键,进入“编辑模式”,才能进行文件的 ...

  8. 【CSS古话今说】-- 01.神奇的CSS-BFC在实战中的应用

    文章首发于掘金 BFC(Block Formatting Context)是Web页面中盒模型布局的CSS渲染模式.它的定位体系属于常规文档流. 想要实现一个BFC布局需要满足以下条件之一: 1.fl ...

  9. SAP Fiori里的List是如何做到懒加载Lazy load的

    今天一同事问我这个问题:S/4HANA Fiori应用里的列表,一旦Scroll到底部就会自动向后台发起新的请求把更多的数据读取到前台显示. 以Product Master这个应用为例,我点击搜索之后 ...

  10. HDU 4871 Shortest-path tree

    先用dijkstra把最短路树建出来,然后就是树的质心分治了. 经过k个点的路径,要么全在子树上,要么经过根结点,因此可以分治. 如果分治的时候选点不好会变成O(n^2),比较极端的情况是比如树是一条 ...