以下代码来自教程(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 框架实现中使用这种设计模式.谢谢!
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.
这篇关于注册表设计模式...好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽