Why singleton steeper global?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
52 views
Actually all terribly spit and turn green at the sight of global variables, and advocating for the use of them is Singleton-s.

Exactly why?

p.s. topic without holivarnaya, just wondering now on the crest of this fashion will be here to clean up the globals (template, user, config)
by | 52 views

7 Answers

0 like 0 dislike
Indeed, the concept of a bit from the "which is better, Alla Pugacheva, or a color TV". However, if I understand correctly the idea the author will try to answer :)
\r
Depends on the language used. But in General once seen such reasons:
\r
1. A global variable can accidentally override another module. Get difficult to catch the error. Class, through which implemented the singleton pattern in most languages you can't override. Well, at least you can get a compilation error (in compiled languages).
\r
2. Scope of class names is usually not context-dependent. Global scope variables in some languages again may vary.
by
0 like 0 dislike
Because the Singleton class, it is possible to hang provisioning, deferred creation of an object or implement thread safety. And global is just a variable that is either readonly or not.
by
0 like 0 dislike
If singleton is used as a global variable, it is no better than a global variable.
Even so. It is a global variable, and do... break is a goto.
Syntactic sugar should not detract from the essence of the phenomenon.
by
0 like 0 dislike
Brad, you gave the link above on the description of the pattern, globals solve another problem — to-end data transfer, OOP name it Registry (the Registry). The registry is exactly the same curve crutch violate the architecture as globally, like GOTO, it is better not to use them. You can replace the transmission in the constructor injection using the set or the IoC.
This is a good article wiki.agiledev.ru/doku.php?id=ooad:manage_dependencies_in_php_code
by
0 like 0 dislike
singleton, it's much easier/safer.
There are languages in which the initialization order of global objects is not defined (C++ for example), in the end, if we have two global object in different files, and if some of them in the constructor calls methods on the second object, we have exactly 50% receive the error. Comes to the aid of singleton.
Improves citability code. Sometimes not clear where this global object (extends it by connecting a header file), you have to scour the headers and learn that as a Yes (that is, if your code reads another programmer), and you wrote in the constructor something like foo = Foo::getInstance();, once it becomes clear.
Finally, it is more consistent with the principle of the PLO, when everything is hidden in the classes/structures, and there is nothing outside of them (no global functions or variables).
by
0 like 0 dislike
Um... Singleton design pattern, global — scope variable (if "in short about the main thing"). It would seem that what is common between these concepts?
by
0 like 0 dislike
Yes, it is nothing not cool. Just global variable in the object wrapper. He was a troublemaker:
— is it hard to replace the plug (MOK) during testing;
— global be not only singleton but all of that is possible through it to obtain
— problems when developing parallel programs (but in php is usually small concerns)
Read about it from Misko Hevery, it is much better than I write about it.
\rmisko.hevery.com/2008/08/25/root-cause-of-singletons/
\rmisko.hevery.com/2008/08/17/singletons-are-pathological-liars/
\rmisko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/
To replace singletons, and registry (Registry) will help the dependency injection Dependency Injection (DI), but that's a topic for another discussion :)
\rmisko.hevery.com/2009/01/14/when-to-use-dependency-injection/
In short, all the dependencies must be provided to the object, not to reach them. There are also frameworks to help in this, for example, Phemto and component in Symfony.
by

Related questions

0 like 0 dislike
7 answers
0 like 0 dislike
7 answers
asked Mar 24, 2019 by azverin
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
26,454 users