单身人士有多糟? [英] How Bad Are Singletons?

查看:87
本文介绍了单身人士有多糟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以....

显然有很多问题都被问及单例,全局状态变量和所有伟大的东西。我的问题是,

There are obviously many questions that all have been asked about Singletons, Global State Variables, and all that great stuff. My question is,

如果单身人士和全球人都这么坏,为什么经常使用?

以下例子只是我头脑中的一些例子,我相信它们是由一大群人使用的。

The following examples are simply ones off the top of my head that I believe are used by a good chunk of people.

我给你一个函数使用psuedo-singleton函数的CodeIgniter:

I give you a function from CodeIgniter that uses a psuedo-singleton function:

(system \codeigniter\Common.php第89行)

(system\codeigniter\Common.php Line 89)

/**
* Class registry
*
* This function acts as a singleton.  If the requested class does not
* exist it is instantiated and set to a static variable.  If it has
* previously been instantiated the variable is returned.
*
* ......
*/
function &load_class($class, $instantiate = TRUE)
{
    static $objects = array();

    // Does the class exist?  If so, we're done...
    if (isset($objects[$class]))
    {
        return $objects[$class];
    }
  .......
}

通过将每个对象放在单个注册表中,您不能使用他们的load_class函数来创建任何东西的多个实例。当你想使用类作为数据结构时,这是特别不方便的。

By placing every object into a single registry, you can't use their load_class function to create multiple instances of anything. This is especially inconvenient when you want to use classes as data structures.

此外,因为所有这些类只有一个实例,所以它导致反对全局状态的参数。这导致我.....

Also, because there is only one instance of all those classes, it leads into the argument against Global State. Which leads me to .....

整个Wordpress系统,主要运行在全局变量。

The entire Wordpress System, which runs mainly on global variables. All of the data for looping through posts is strewn about in various globals.

(wp-include \query.php第2644行)

(wp-includes\query.php Line 2644)

/**
 * Setup global post data.
 *
 *....
 */
function setup_postdata($post) {
    global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;

    $id = (int) $post->ID;

    $authordata = get_userdata($post->post_author);
    ....
}

这些只是框架的两个主要示例使用Singleton / Globals作为他们整个系统的基础!

These are just two main examples of Frameworks that use Singleton/Globals as the basis for their entire system!

那么,是因为这些系统还没有跟上OOP的方法论?如果你有这么多人告诉你不要使用全局变量或单例,那么就没有意义。根据所述的做法,你的整个系统都可以使用。

So .. is it just because these systems haven't caught up to OOP methodology? It just doesn't make sense when you have so many people telling you not to use Global Variables or Singletons, to make your entire system based on said practices.

课程是关于向后兼容PHP4的论点。我仍然认为有在PHP4中做OOP编程的方法,因为类仍然可用。

There of course is the argument about backwards-compatibility with PHP4. I still think that there were ways to do OOP programming in PHP4, as classes were still available.

推荐答案

因为使用单例工作相对容易,而且没有对应用程序结构进行更详细的规划。我询问了有关替代品的问题一段时间以前,并得到有趣的答案。

Because it's relatively easy to work with singletons, and working without takes much more detailed planning of your application's structure. I asked a question about alternatives some time ago, and got interesting answers.

这篇关于单身人士有多糟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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