Many have heard about the "singleton" programming pattern. But in practice I never had to meet him in action (and maybe he did, but I don’t guess about that). Tell us what it is (I know how to use Russian, but besides dry terminology and a pair of non-real-life I did not find any examples), how, and most importantly in what cases they can / should be used, well, preferably an example (in Ruby , if I may).
2 answers
A singleton is a pattern that describes an object in a single copy, without the possibility of creating a copy of it. For example, you have settings for the application and they must be in a single copy, it is not allowed to create a copy of such an object, change it in one part of the application, and the other will not change the old copy. Or by establishing a connection to the database once, you need to use the already established connection and not create it again. Instead of a database, there may be a log file or any other repository existing in a single copy.
In order to create a singleton in Ruby, you must close the possibility of creating an object through new and cloning an object (methods clone , dup , _dup ). In the standard Ruby library, a module has already been implemented; by mixing it into its own class, you can turn it into a singleton by including it in the class.
class MyClass include Singleton # ... end o = MyClass.instance - And where does the .instance method come from? Is the singlet module adding it? Or does each class have it? - smellyshovel
- And it turns out that now you cannot create an object of class MyClass, but you can only use this class, as if it is an object itself? - smellyshovel
- And again: what is the danger, for example, leaving possible the creation of instances of the class of loggers or database connections? What's the big deal? - smellyshovel
- @MatveyMamonov (a) is mixed by the module, (b) well, it really is, but this is an extra measure to ensure correct work with the code, yes, (c) for example, it’s unpleasant to write in one cache, and read from another, cache it does not work; but in the general case they may not be, moreover, singletons as a pattern are considered by many to be evil because of the assumptions that "this can not be more than one", which is rare, but bites very painfully in the future. - D-side
- @ D-side Everything is clear and to the point, thank you. - smellyshovel
It is important to understand that, in general, "OOP patterns" are crutches that have historically arisen to solve certain problems of certain programming languages , i.e. not a goal, but a means. Naturally, in all OOP languages, things are different, and for example, Singleton, as something special, does not have an urgent need for Ruby at all.
There is the concept of "static field (attribute)" - through it, instances of a class can communicate with each other or be configured from the outside all at once. If you think of this as a special instance, which is originally and it is always one. You may want to pass it somewhere, but some programming languages do not allow passing a class as function arguments — the accepted crutch for this problem was Singleton, but you can transfer a class to Ruby .
Static fields and class methods do a great job with the rest, because in fact they are ordinary fields and methods of the metaclass - an implicit class that Ruby creates for each obvious one, imperceptibly for the user, instantiating it once, then hanging on it the methods that in your opinion hang on explicitly certain class, but "statically". This in fact already implements Singleton .
The remaining fabrications, such as the Singleton module, exist in the standard rubies library only so that people who come from other programming languages can write the code visually, as they are used to, but do not need to "write in the language And as if it were B" - - use the tool correctly, manage with the fields and methods of the class.
Code samples and some more explanations about metaclasses:
- I will add that when a class instance is expanded by methods, this class is called a singlet class. The only thing that we do not lose the possibility of its cloning and to obtain pure Singleton, you must remove the cloning methods yourself. - cheops
- Links were helpful, thanks - smellyshovel
- @cheops is a metaclass . - D-side