Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Bad Are Singletons?

So ....

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

If Singletons and Globals are So Bad, Why are they used so often?

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

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

(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];
    }
  .......
}

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 .....

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-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);
    ....
}

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

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.

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.

like image 813
Tyler Carter Avatar asked Jan 03 '10 20:01

Tyler Carter


People also ask

What's the downside of singleton?

Singletons hinder unit testing: A Singleton might cause issues for writing testable code if the object and the methods associated with it are so tightly coupled that it becomes impossible to test without writing a fully-functional class dedicated to the Singleton.

Are singletons bad Swift?

Singletons are not universally bad, but in many situations they come with a set of problems that can be avoided by creating more well-defined relationships between your objects and by using dependency injection.

Should singletons be used?

A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.

Why singleton is bad for testing?

It's very difficult to write unit tests for code that uses singletons because it is generally tightly coupled with the singleton instance, which makes it hard to control the creation of singleton or mock it.


1 Answers

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.

like image 188
Pekka Avatar answered Sep 28 '22 02:09

Pekka