注册表设计模式......好还是坏?

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)