gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:16:43 25 4
gpt4 key购买 nike

以下代码来自教程(http://net.tutsplus.com/php/creating-a-php5-framework-part-1/),不是我的。

我有几个关于这段代码的问题...

  • 文章声称它正在使用“注册表设计模式”;这是该设计在​​行业中的通用名称吗?
  • 有没有其他类似的更好的选择?
  • 这种模式是否被认为是在 MVC 框架的上下文中实现的良好实践?

我只是想弄清楚是否应该在我自己的 MVC 框架实现中使用这种设计模式。谢谢!

<?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 可以称为注册表。

Is there another similar patter out there that would be a better option?

注册表有两种变体。有全局注册表(这是最常见的,这是其中的一个示例)。并且有一个本地注册表。本地注册表传递给需要它的对象,而不是通过全局符号(静态类、单例等)获得。本地注册表的耦合度较低,但也稍微抽象一些,因此需要权衡。

您还可以走得更远,使用完全依赖注入(inject),将所有依赖显式传递给需要它们的对象。这在较大的应用程序中可能有点乏味。您可以将其与依赖项注入(inject)容器结合使用,这是一段“知道”哪些类具有哪些依赖项的代码。这比本地注册表更复杂,但耦合度非常低。

Is this pattern considered to be good practice to implement in the context of an MVC framework?

这是常见的做法。是好是坏是判断电话。就我个人而言,我愿意接受一些复杂性作为去耦的返回,但是 ymmv。

关于php - 注册表设计模式...好还是坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1151341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com