Registry design pattern...good or bad?(注册表设计模式...好还是坏?)
问题描述
以下代码来自教程(http://net.tutsplus.com/php/creating-a-php5-framework-part-1/),不是我的.
The following code is from a tutorial (http://net.tutsplus.com/php/creating-a-php5-framework-part-1/), not mine.
我有一些关于这段代码的问题...
I have a few questions about this code...
- 文章声称它使用了注册表设计模式";这是该设计在行业中的通用名称吗?
- 还有其他类似的模式会是更好的选择吗?
- 这种模式是否被认为是在 MVC 框架上下文中实施的良好做法?
我只想弄清楚是否应该在我自己的 MVC 框架实现中使用这种设计模式.谢谢!
I just want to figure out if I should use this design pattern in my own implementation of a MVC framework. Thanks!
<?php
/**
* The PCARegistry object
* Implements the Registry and Singleton design patterns
* @version 0.1
* @author Michael Peacock
*/
class PCARegistry {
/**
* Our array of objects
* @access private
*/
private static $objects = array();
/**
* Our array of settings
* @access private
*/
private static $settings = array();
/**
* The frameworks human readable name
* @access private
*/
private static $frameworkName = 'PCA Framework version 0.1';
/**
* The instance of the registry
* @access private
*/
private static $instance;
/**
* Private constructor to prevent it being created directly
* @access private
*/
private function __construct()
{
}
/**
* singleton method used to access the object
* @access public
* @return
*/
public static function singleton()
{
if( !isset( self::$instance ) )
{
$obj = __CLASS__;
self::$instance = new $obj;
}
return self::$instance;
}
/**
* prevent cloning of the object: issues an E_USER_ERROR if this is attempted
*/
public function __clone()
{
trigger_error( 'Cloning the registry is not permitted', E_USER_ERROR );
}
/**
* Stores an object in the registry
* @param String $object the name of the object
* @param String $key the key for the array
* @return void
*/
public function storeObject( $object, $key )
{
require_once('objects/' . $object . '.class.php');
self::$objects[ $key ] = new $object( self::$instance );
}
/**
* Gets an object from the registry
* @param String $key the array key
* @return object
*/
public function getObject( $key )
{
if( is_object ( self::$objects[ $key ] ) )
{
return self::$objects[ $key ];
}
}
/**
* Stores settings in the registry
* @param String $data
* @param String $key the key for the array
* @return void
*/
public function storeSetting( $data, $key )
{
self::$settings[ $key ] = $data;
}
/**
* Gets a setting from the registry
* @param String $key the key in the array
* @return void
*/
public function getSetting( $key )
{
return self::$settings[ $key ];
}
/**
* Gets the frameworks name
* @return String
*/
public function getFrameworkName()
{
return self::$frameworkName;
}
}
?>
推荐答案
文章声称它使用了注册表设计模式";这是这个设计在业界的通用名称吗?
The article claims it is using the "registry design pattern"; is that the universal name for this design in the industry?
是的,但实施显然可能不同.基本上,注册表是共享对象的容器.在真正的基本版本中,您可以使用数组.因此,变量 $GLOBALS
可以称为注册表.
Yes, but the implementation could obviously differ. Basically, a registry is a container for shared objects. In the really basic version, you could use an array. As such, the variable $GLOBALS
could be called a registry.
还有其他类似的模式会是更好的选择吗?
Is there another similar patter out there that would be a better option?
注册表有两种变体.有全局注册表(这是最常见的,这是一个例子).并且有一个本地注册表.本地注册表传递给需要它的对象,而不是通过全局符号(静态类、单例等)获取.本地注册中心的耦合度较低,但也稍微抽象一些,因此需要权衡.
There are two variations of a registry. There is the global registry (Which is far the most common, and which this is an example of). And there is a local registry. A local registry is passed to objects that need it, rather than obtained through a global symbol (static class, singleton etc.). A local registry has a lower degree of coupling, but is also slightly more abstract, so there is a tradeoff there.
您还可以更进一步,使用完全依赖注入,将所有依赖显式地传递给需要它们的对象.这在较大的应用程序中可能有点乏味.你可以将它与依赖注入容器结合起来,这是一段知道"哪些类具有哪些依赖项.这比本地注册表更复杂,但耦合度非常低.
You can also go even further and use full dependency injection, where you explicitly pass all the dependencies to the objects that need them. This can be a bit tedious in larger applications. You can couple this with a dependency injection container, which is a piece of code that "knows" which dependencies which classes have. This is even more complex than a local registry, but has a very low degree of coupling.
这种模式是否被认为是在 MVC 框架上下文中实施的良好实践?
Is this pattern considered to be good practice to implement in the context of an MVC framework?
这是常见的做法.是好是坏是判断力.我个人愿意接受一些复杂性以换取解耦,但 ymmv.
It's common practise. If it's good or bad is a judgement call. Personally I'm willing to accept some complexity in return of decoupling, but ymmv.
这篇关于注册表设计模式...好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:注册表设计模式...好还是坏?
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01