D
D
Denis Safronov2010-09-09 22:31:24
PHP
Denis Safronov, 2010-09-09 22:31:24

Why is singleton better than global?

Actually, everyone terribly spits and turns green at the sight of global variables and advocates using Singletons instead.
Actually why?
ps the topic is not holy, just wondering, now on the crest of this fashion I will clean up the globals (template, user, config)

Answer the question

In order to leave comments, you need to log in

14 answer(s)
A
Alexander, 2010-09-09
@0lympian

Indeed, there are few concepts from the series “which is better, Alla Pugacheva, or a color TV”. However, if I understand the author's idea correctly, I'll try to answer :)
Everything depends very much on the language used. But in general, the following reasons immediately appear:
1. A global variable can be accidentally overridden by another module. We get a hard-to-catch error. The class by which the singleton pattern is implemented in most languages ​​cannot be redefined. Well, or at least you can get a compilation error (in compiled languages).
2. The scope of class names is usually context independent. The global scope of variables in some languages, again, may change.

O
OpenMinded, 2010-09-09
@OpenMinded

Since Singleton is a class, you can hang initialization, lazy object creation, or implement thread safety on it. And global is just a variable that is either readonly or not.

M
Mark, 2010-09-10
@printf

If singleton is used as a global variable, it is no better than a global variable.
Not even like that. It is a global variable, just as the do...break construct is a goto.
Syntactic sugar should not distract from the essence of the phenomenon.

V
Vyacheslav Plisko, 2010-09-09
@AmdY

Brad, you were given a link above to a description of the pattern, globals solve another problem - end-to-end data transfer, in OOP its name is Registry (Registry). The registry is exactly the same crooked crutch that breaks the architecture like globals, like GOTO, it's better not to use them. It can be replaced by passing to the constructor, injection via set or IoC.
Here is a good article wiki.agiledev.ru/doku.php?id=ooad :manage_dependencies_in_php_code

A
Artemzr, 2010-09-10
@Artemzr

singleton is still more convenient/safer.
There are languages ​​in which the order of initialization of global objects is not defined (with ++ for example), as a result, if we have two global objects in different files, and if one of them calls the methods of the second object in the constructor, then we have exactly 50% of the receipt errors. Singleton comes to the rescue. And finally, this is more in line with the oop principle, when everything is hidden in classes / structures, and there is nothing outside them (no global functions or variables).
Improved code readability. Sometimes it’s not entirely clear where this global object came from (it is distributed by including a header file), you have to scour the headers and find out what and how (this is if another programmer reads your code), and so - you wrote something like foo in the constructor = Foo::getInstance(); will immediately become clear.

C
Chvanikoff, 2010-09-09
@Chvanikoff

Umm ... Singleton is a design pattern, global is the scope of a variable (if "in short about the main thing"). It would seem that what is common between these concepts?

A
Alexey Shein, 2010-09-11
@conf

Yes, he is not cooler than anything. Just a global variable in an object wrapper. From him there are only problems:
- it's hard to replace with a stub (mock) when testing;
- not only the singleton itself becomes global, but also everything that can be obtained through it
- problems in the development of parallel programs (but in php this is usually of little concern)
Read Misko Hevery about this, he is much better than I write about it.
misko.hevery.com/2008/08/25/root-cause-of-singletons/
misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
misko.hevery.com/2008/08/ 21/where-have-all-the-singletons-gone/
Dependency Injection (DI) will help replace singletons and the Registry, but this is a topic for another conversation :)
misko.hevery.com/2009/01/14/when-to-use-dependency-injection/
In short, all dependencies should be provided to the object, not pulled by it. There are also frameworks to help with this, such as Phemto and Symfony component.

N
NeOn4eG, 2010-09-09
@NeOn4eG

I'm sorry, but what is a Singleton?

F
fenrirgray, 2010-09-10
@fenrirgray

I don’t understand what has singletons to do with it ...
Why global variables should be avoided varies from language to language. For example, because they can cause difficult-to-diagnose errors. Or because global variables hang in memory all the time, and local variables are released when the function exits.

N
noRerih, 2010-09-10
@noRerih

It seems strange to me to pose the question with the definition of "cooler". Some programming practices are better than others, but I don't think patterns measure coolness with variables. Although programmers do not measure each other with anything. In the sense of cool :))
In any case, sit down to rewrite the code and make changes, because “it’s so cool” - you shouldn’t, you must understand exactly what you are changing and why you are doing it. In other words, the unconscious use of a singleton can be even worse.

D
Denis Safronov, 2010-09-10
@mcdb

Thanks for the clarification, I meant PHP (you can see by the tags). Also thanks for - at [censored]

S
Sergey Belikov, 2010-09-10
@Belikov

In addition to lazy initialization, a singleton also allows you to create objects of different classes depending on conditions.
If we make a class for interacting with the database a singleton, it will be possible, for example, to create an object for working with the local cache in offline mode, and an object for working with the database online. You can even imperceptibly for an external user to replace these objects with or without a connection. Although this is already more difficult, because. it will be necessary to catch errors when trying to access the database.
There are simply no global variables in C#. :)

H
hamMElion, 2010-09-10
@hamMElion

singleton is a class that can have at most one instance.
prototype is a class that can have any number of instances.
global variables - variables available anywhere in the program.
“everyone spits terribly and turns green at the sight of global variables”; love OOP.

A
Artem Bondarenko, 2010-09-10
@mr_avi

If only because a singleton is not just a store of public variables, but also a full-fledged class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question