久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

注冊表設計模式...好還是壞?

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.

這篇關于注冊表設計模式...好還是壞?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 国产成人精品网站 | 久久久精品一区 | 北条麻妃av一区二区三区 | 日韩视频一区二区 | 精品成人免费一区二区在线播放 | 涩涩导航| 成人午夜精品 | 国产日韩精品视频 | 国产区一区二区三区 | 亚洲成人免费av | 亚洲最新在线视频 | 国产有码| 国产精品毛片一区二区在线看 | 国产精品乱码一二三区的特点 | 亚洲精品久久久久久久久久久久久 | 精品一二区| 色欧美日韩 | 亚洲精品中文字幕在线 | 精品久久影院 | 亚洲午夜av| 亚洲精品欧美一区二区三区 | 日韩视频免费 | 亚洲国产精品日韩av不卡在线 | 高清久久 | 91精品久久久 | 亚洲3级| 国产精品日本一区二区在线播放 | 草久视频 | 国产精品日韩一区二区 | 一二区视频| 亚洲国产精品久久久 | 国产欧美日韩一区二区三区 | 午夜不卡福利视频 | 日本视频免费 | 黑人巨大精品欧美黑白配亚洲 | 久久精品亚洲精品国产欧美kt∨ | 国产精品1区 | 午夜影院操| 亚洲视频一区在线观看 | 精产国产伦理一二三区 | 国产视频一区二区在线观看 |