What is the advantage of Singleton Design Template

everyone knows how to write code for Singleton Design Pattern.say for example

public class Singleton { // Private static object can access only inside the Emp class. private static Singleton instance; // Private empty constructor to restrict end use to deny creating the object. private Singleton() { } // A public property to access outside of the class to create an object. public static Singleton Instance { get { if (instance == null) { instance = new Singleton(); } return instance; } } } 

it is very clear that when we create an instance of any class, allocating memory for each instance many times, but in the case of the Singleton design pattern, one instance provides a service for all calls.

1) I'm a little confused and really don't understand what the reasons are ... what, when to go for the Singleton design pattern. just to save some memory or any other advantage there.

2) suppose that in any one program there can be many classes, which classes should follow the Singleton design pattern? What is the advantage of the Singleton design pattern?

3 in real applications, when do you need to do any classes after the Singleton design pattern? thanks

Here is a thread safe singleton

 public sealed class MultiThreadSingleton { private static volatile MultiThreadSingleton instance; private static object syncRoot = new Object(); private MultiThreadSingleton() { } public static MultiThreadSingleton Instance { get { if (instance == null) { lock (syncRoot) { if (instance == null) { instance = new MultiThreadSingleton(); } } } return instance; } } } 
+6
source share
6 answers

Each instance of an object provides only the same instance of the object.

Take the scenario, say, for a company application, there is only one CEO. If you want to create or access a CEO object, you must return the same CEO object each time.

Another one, after entering the application, the current user must return the same object every time.

+8
source

Benefits of the Singleton Template:

• Instance management: Singleton prevents other objects from creating their own copies of the Singleton object, ensuring that all objects access the same instance.

• Flexibility. Because the class manages the instantiation process, the class has the flexibility to modify the instantiation process.

The advantage of Singleton over global variables is that you are absolutely sure about the number of instances when using Singleton, and you can change your mind and control any number of instances.

+3
source

Other answers are good too. But they provide examples of the behavioral characteristics of the pattern. But Singleton is more about creation . Thus, one of the most important advantages of the template is that it is resource friendly. You do not waste memory on a new object when you really do not need a new one.

This leads to another benefit that eliminates overhead.

+3
source

Use / use of the real-time Singleton design pattern.

  • When using multithreading to manage a multithreaded pool.
  • to manage host service repositories in SOA (Service Oriented Architecture).
  • to implement a registration platform
  • in automation Testing / Testing a project block, that is, projects with a coded user interface.
  • When implementing caching in a large application.
  • to configure for proper application management.
+2
source

As a rule, singleton is considered an anti-pattern in OOP, because this means that the class claims that in relation to the whole program - in OOP, it should not know - it knows that it will be the only one. This suggests that singleton is the right way to implement a constant in my experience. In general, if something I was going to hardcode into a program (say, the database username), then it can be transferred to a configuration file or singleton.

One of the few areas where Java is superior to C # (in my opinion ...) is enumeration support. Java offers truly OO constants through enums, and so I will always implement singleton in Java. C # does not have a ready-made equivalent.

+1
source

One useful place to use singleton is to access a resource that requires only one access point. For example, I used it when writing code to talk to a device. I want one piece of code to talk to the device, so I use singleton. Any attempt to create another instance of an object that is talking to the device will simply give you the same object back, so I never have to worry about two instances that support unsynchronized data about the device or receive messages on the device and from the device up or out.

But, of course, you are not required to use them. It is just a tool that is sometimes useful.

0
source

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


All Articles