Creating a static link compared to a singleton

I am using java driver for mongodb and the documentation says:

"you must create one instance of Mongo, and you can use it in every request.

Using one instance sounds like a single.

In other places for another library, I read the instructions saying that I should create a static link because it is thread safe.

Can someone explain the differences between singleton and creating a static link?

Thus, the actual code that I need to create a static or single singlet will be as follows:

Mongo m = new Mongo( "localhost" , 27017 ); 

Can someone explain both methods and the main differences, if any?

+6
source share
5 answers

Java typically uses a static variable to implement the Singleton pattern.

http://java.sun.com/developer/technicalArticles/Programming/singletons/

+4
source

You have 3 problems: singleton, static link, and thread safety.

You have singleton if you can only create one instance of a class. This is useful since everything will be confusing if you have two instances of Mongo. However, you cannot implement the singleton design template for Mongo in your code: you can call new Mongo() anywhere and create as many instances as you want. You just have to be careful, you do not do this, but it should not be very difficult.

To implement a singleton, a class developer very often uses a static link as follows:

 public class MyClass { private static final MyClass SINGLETON = new MyClass(); private MyClass() {...} // !!private, not accessible public MyClass getSingleton() { return SINGLETON; } } 

And you will only have one instance of MyClass , since the constructor is private, and the only way to get the instance is through MyClass.getSingleton() . Obviously, a Mongolian designer would have to develop the Mongo class as such; you cannot do anything to make him solitary.

As for thread safety, I don't quite understand the connection with singleton. The singleton class must be thread safe: if many threads change and read the state of a singleton, you need the correct synchronization to make sure that all threads see the same values. I do not know Mongo, but I would argue that this is a class protected by threads.

+3
source

Singleton is a design pattern in which you have one instance of an object shared between the rest of your code. Static variables are a Java language.

To implement Singleton, you usually use static variables.

+2
source

Use the Singleton pattern for an object if you only want to bypass one instance of this object with other objects and methods that should use this single instance. Use a static link if you want to only statically use an object class (for example, a static link).

0
source

If only one class uses your singleton object, then there are no visible differences in the number of objects created.

Assuming you need a classASing object using the Singleton approach

 ClassASing { private static ClassASing obj = new ClassASing(); private ClassAsing(){...} public static ClassASing getNewObject(){ return obj; } } 

Using the Singleton Approach

 ClassB{ private ClassASing singObj = ClassASing.getNewObject(); } 
  • no matter how many instances of ClassB all of them created, the same ClassASing object will be used

using static link

 ClassB{ private static ClassA sObj = new ClassA(); } 

* regardless of how many instances of ClassB all of them created, the same link will be used pointing to the same object.

There are not many differences. 1 object is used in both cases.

Now, if u considers another sencario, you need an object in your two classes.

singleton approach

 ClassB1{ private ClassASing singObj1 = ClassASing.getNewObject(); } ClassB2{ private ClassASing singObj2 = ClassASing.getNewObject(); } 
  • no matter how many instances of ClassB1 all of them created, the same ClassASing object will be used
  • no matter how many instances of ClassB2 all of them created, the same ClassASing object that ClassB1 already uses will be used, so there is only one ClassASing object

Static Reference Approach

 ClassB1{ private static ClassA sObj1 = new ClassA(); } ClassB2{ private static ClassA sObj2 = new ClassA(); } 
  • no matter how many instances of ClassB1 all of them created, the same sobj1 link pointing to the same object will be used
  • no matter how many instances of ClassB2 all of them created, the same sobj2 link pointing to the same object will be used, but this object is different from the one created in ClassB1, so now you have two ClassA objects .
0
source

Source: https://habr.com/ru/post/902947/


All Articles