- 单例模式
Yii2中实现的单例并非严格意义上的单例(私有化构造函数,clone,序列化等),而是采用serviceLocator+Container实现的单例模式。
\yii\di\ServiceLocator::get/** * Returns the component instance with the specified ID. * * @param string $id component ID (e.g. `db`). * @param boolean $throwException whether to throw an exception if `$id` is not registered with the locator before. * @return object|null the component of the specified ID. If `$throwException` is false and `$id` * is not registered before, null will be returned. * @throws InvalidConfigException if `$id` refers to a nonexistent component ID * @see has() * @see set() */ 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; } else { return $this->_components[$id] = Yii::createObject($definition); } } elseif ($throwException) { throw new InvalidConfigException("Unknown component ID: $id"); } else { return null; } }
\yii\di\Container::get
/** * Registers a class definition with this container and marks the class as a singleton class. * * This method is similar to [[set()]] except that classes registered via this method will only have one * instance. Each time [[get()]] is called, the same instance of the specified class will be returned. * * @param string $class class name, interface name or alias name * @param mixed $definition the definition associated with `$class`. See [[set()]] for more details. * @param array $params the list of constructor parameters. The parameters will be passed to the class * constructor when [[get()]] is called. * @return static the container itself * @see set() */ public function setSingleton($class, $definition = [], array $params = []) { $this->_definitions[$class] = $this->normalizeDefinition($class, $definition); $this->_params[$class] = $params; $this->_singletons[$class] = null; return $this; }
- 工厂方法模式
- 抽象工厂
\yii\BaseYii::createObject这个方法更像是一个万能的工厂,不管给啥都能给你生产出相应的对象。 - 模板方法模式
模板方法定义:定义一个操作中的算法的框架,而将一些步骤延迟到子类中,使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 在Yii2中大量使用了模板方法模式。 如\yii\caching\Cache类中 /** * Retrieves a value from cache with a specified key. * @param mixed $key a key identifying the cached value. This can be a simple string or * a complex data structure consisting of factors representing the key. * @return mixed the value stored in cache, false if the value is not in the cache, expired, * or the dependency associated with the cached data has changed. */ public function get($key) { $key = $this->buildKey($key); $value = $this->getValue($key); if ($value === false || $this->serializer === false) { return $value; } elseif ($this->serializer === null) { $value = unserialize($value); } else { $value = call_user_func($this->serializer[1], $value); } if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->getHasChanged($this))) { return $value[0]; } else { return false; } } /** * Retrieves a value from cache with a specified key. * This method should be implemented by child classes to retrieve the data * from specific cache storage. * @param string $key a unique key identifying the cached value * @return string|boolean the value stored in cache, false if the value is not in the cache or expired. */ abstract protected function getValue($key);
- 建造者模式
建造者模式也称生成器模式,核心思想是将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示。$config = yii\helpers\ArrayHelper::merge( require(__DIR__ . '/../../common/config/main.php'), require(__DIR__ . '/../../common/config/main-local.php'), require(__DIR__ . '/../config/main.php'), require(__DIR__ . '/../config/main-local.php') ); $application = new yii\web\Application($config); $application->run();
yii\web\Application 类使用了建造者模式,我们不用逐一去配置应用程序的每个配置项, 通过yii\helpers\ArrayHelper::merge将所有的配置项合并到一起统一配置。
- 代理模式
Yii2中的behavior也采用了代理模式。 - 原型模式
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
\yii\db\ActiveRelationTrait::__clone - 中介者模式
- 命令模式
Yii2中的Logger采用了命令模式。
\yii\log\Logger::flush/** * Flushes log messages from memory to targets. * @param boolean $final whether this is a final call during a request. */ public function flush($final = false) { $messages = $this->messages; // https://github.com/yiisoft/yii2/issues/5619 // new messages could be logged while the existing ones are being handled by targets $this->messages = []; if ($this->dispatcher instanceof Dispatcher) { $this->dispatcher->dispatch($messages, $final); } }
通过dispatcher的dispatch方法分发给不同的target进行处理
\yii\log\Dispatcher::dispatch/** * Dispatches the logged messages to [[targets]]. * @param array $messages the logged messages * @param boolean $final whether this method is called at the end of the current application */ public function dispatch($messages, $final) { $targetErrors = []; foreach ($this->targets as $target) { if ($target->enabled) { try { $target->collect($messages, $final); } catch (\Exception $e) { $target->enabled = false; $targetErrors[] = [ 'Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToString($e), Logger::LEVEL_WARNING, __METHOD__, microtime(true), [], ]; } } } if (!empty($targetErrors)) { $this->dispatch($targetErrors, true); } }
不同的target通过继承Target类,覆写collect方法进行不同的处理。
\yii\log\Target::collect/** * Processes the given log messages. * This method will filter the given messages with [[levels]] and [[categories]]. * And if requested, it will also export the filtering result to specific medium (e.g. email). * @param array $messages log messages to be processed. See [[Logger::messages]] for the structure * of each message. * @param boolean $final whether this method is called at the end of the current application */ public function collect($messages, $final) { $this->messages = array_merge($this->messages, $this->filterMessages($messages, $this->getLevels(), $this->categories, $this->except)); $count = count($this->messages); if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) { if (($context = $this->getContextMessage()) !== '') { $this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME]; } // set exportInterval to 0 to avoid triggering export again while exporting $oldExportInterval = $this->exportInterval; $this->exportInterval = 0; $this->export(); $this->exportInterval = $oldExportInterval; $this->messages = []; } }
- 责任链模式
Yii2中的behavior采用了责任链模式的过滤器模式。 - 装饰模式
装饰模式定义:动态地给一个对象添加一些额外的职责。
yii2中的behavior中使用了装饰模式,因为通过behavior给原来的对象增加了额外的职责。
\yii\base\Component/** * Makes sure that the behaviors declared in [[behaviors()]] are attached to this component. */ public function ensureBehaviors() { if ($this->_behaviors === null) { $this->_behaviors = []; foreach ($this->behaviors() as $name => $behavior) { $this->attachBehaviorInternal($name, $behavior); } } } /** * Attaches a behavior to this component. * @param string|integer $name the name of the behavior. If this is an integer, it means the behavior * is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name * will be detached first. * @param string|array|Behavior $behavior the behavior to be attached * @return Behavior the attached behavior. */ private function attachBehaviorInternal($name, $behavior) { if (!($behavior instanceof Behavior)) { $behavior = Yii::createObject($behavior); } if (is_int($name)) { $behavior->attach($this); $this->_behaviors[] = $behavior; } else { if (isset($this->_behaviors[$name])) { $this->_behaviors[$name]->detach(); } $behavior->attach($this); $this->_behaviors[$name] = $behavior; } return $behavior; }
\yii\base\Behavior
/** * Attaches the behavior object to the component. * The default implementation will set the [[owner]] property * and attach event handlers as declared in [[events]]. * Make sure you call the parent implementation if you override this method. * @param Component $owner the component that this behavior is to be attached to. */ public function attach($owner) { $this->owner = $owner; foreach ($this->events() as $event => $handler) { $owner->on($event, is_string($handler) ? [$this, $handler] : $handler); } }
- 策略模式
定义一组算法,将每个算法都封装起来,并且使它们之间可以互换
\yii\base\Security::generatePasswordHash内采用了策略模式 - 适配器模式
- 迭代器模式
class SessionIterator implements \Iterator class CookieCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable class HeaderCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable 还有很多*Collectoin类都实现了迭代器模式
- 组合模式
- 观察者模式
Yii2中的behavior和event实现了观察者模式 - 门面模式
Yii2中的cache采用了门面模式,如\yii\caching\Cache::set对外提供了统一的设置缓存的方法,通过模板方法\yii\caching\Cache::setValue在不同的实现内中实现。 - 备忘录模式
- 访问者模式
- 状态模式
\yii\base\Application/** * Runs the application. * This is the main entrance of an application. * @return integer the exit status (0 means normal, non-zero values mean abnormal) */ public function run() { try { $this->state = self::STATE_BEFORE_REQUEST; $this->trigger(self::EVENT_BEFORE_REQUEST); $this->state = self::STATE_HANDLING_REQUEST; $response = $this->handleRequest($this->getRequest()); $this->state = self::STATE_AFTER_REQUEST; $this->trigger(self::EVENT_AFTER_REQUEST); $this->state = self::STATE_SENDING_RESPONSE; $response->send(); $this->state = self::STATE_END; return $response->exitStatus; } catch (ExitException $e) { $this->end($e->statusCode, isset($response) ? $response : null); return $e->statusCode; } }
- 解释器模式
- 享元模式
- 桥接模式
Yii2中behavior、component之间采用了桥接模式/** * Attaches the behavior object to the component. * The default implementation will set the [[owner]] property * and attach event handlers as declared in [[events]]. * Make sure you call the parent implementation if you override this method. * @param Component $owner the component that this behavior is to be attached to. */ public function attach($owner) { $this->owner = $owner; foreach ($this->events() as $event => $handler) { $owner->on($event, is_string($handler) ? [$this, $handler] : $handler); } }
- 空对象模式
- 对象池模式
\yii\db\Connection::open/** * Establishes a DB connection. * It does nothing if a DB connection has already been established. * @throws Exception if connection fails */ public function open() { if ($this->pdo !== null) { return; } if (!empty($this->masters)) { $db = $this->openFromPool($this->masters, $this->masterConfig); if ($db !== null) { $this->pdo = $db->pdo; return; } else { throw new InvalidConfigException('None of the master DB servers is available.'); } } if (empty($this->dsn)) { throw new InvalidConfigException('Connection::dsn cannot be empty.'); } $token = 'Opening DB connection: ' . $this->dsn; try { Yii::info($token, __METHOD__); Yii::beginProfile($token, __METHOD__); $this->pdo = $this->createPdoInstance(); $this->initConnection(); Yii::endProfile($token, __METHOD__); } catch (\PDOException $e) { Yii::endProfile($token, __METHOD__); throw new Exception($e->getMessage(), $e->errorInfo, (int) $e->getCode(), $e); } } /** * Opens the connection to a server in the pool. * This method implements the load balancing among the given list of the servers. * @param array $pool the list of connection configurations in the server pool * @param array $sharedConfig the configuration common to those given in `$pool`. * @return Connection the opened DB connection, or null if no server is available * @throws InvalidConfigException if a configuration does not specify "dsn" */ protected function openFromPool(array $pool, array $sharedConfig) { if (empty($pool)) { return null; } if (!isset($sharedConfig['class'])) { $sharedConfig['class'] = get_class($this); } $cache = is_string($this->serverStatusCache) ? Yii::$app->get($this->serverStatusCache, false) : $this->serverStatusCache; shuffle($pool); foreach ($pool as $config) { $config = array_merge($sharedConfig, $config); if (empty($config['dsn'])) { throw new InvalidConfigException('The "dsn" option must be specified.'); } $key = [__METHOD__, $config['dsn']]; if ($cache instanceof Cache && $cache->get($key)) { // should not try this dead server now continue; } /* @var $db Connection */ $db = Yii::createObject($config); try { $db->open(); return $db; } catch (\Exception $e) { Yii::warning("Connection ({$config['dsn']}) failed: " . $e->getMessage(), __METHOD__); if ($cache instanceof Cache) { // mark this server as dead and only retry it after the specified interval $cache->set($key, 1, $this->serverRetryInterval); } } } return null; }
- 依赖注入
\yii\di\Container - MVC
Yii2框架采用MVC
转载请注明:MitNick » Yii2中使用的设计模式