注册表设计模式好还是坏? [英] Registry design pattern...good or bad?

查看:124
本文介绍了注册表设计模式好还是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码来自教程( 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 dependency to the objects that needs 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.

这篇关于注册表设计模式好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆