How do I write a simple singleton pattern?

Updated on technology 2024-03-14
8 answers
  1. Anonymous users2024-02-06

    First, the basic implementation ideas:

    Singletons are implemented in two steps:

    1. Define the constructor of the class as a private method, so that the ** in other places cannot instantiate the object of the class by calling the constructor of the class, and only obtain the only instance of the class through the static method provided by the class;

    2. Provide a static method in the class, when we call this method, if the reference held by the class is not empty, the reference will be returned, and if the reference kept by the class is empty, an instance of the class will be created and the reference of the instance will be given to the reference kept by the class.

    2. The demonstration is as follows:

    1. Enumeration implements singletons:

    2. Lazy threads are not safe:

    3. Lazy thread safety:

    4. Hungry Han style:

    5. Double check lock:

    6. Static Inner Classes:

  2. Anonymous users2024-02-05

    The singleton pattern ensures that a class has only one instance, instantiates itself, and makes it available globally to the entire system. It doesn't create a copy of the instance, but instead returns a reference to the instance stored inside the singleton class.

    **There are two main ways to write it:

    1. Hungry Han style.

    public class singleton1 ;Private parameterless constructor.

    private static singleton1 instance = new singleton1();

    private static singleton1 getinstance();

    private volatile static singleton2 instance;Plus volite prevents instruction reflow.

    private static singleton2 getinstance(){if (instance == null) {

    synchronized (locking prevents multiple instances from being generated by multiple threads.)

    if (instance == null) {

    instance = new singleton2();The instructions are reordered, and the assignment is completed first, but the constructor has not yet been executed.

    return instance;

    Double-checked, thread-safe.

  3. Anonymous users2024-02-04

    In general, there are several forms of singleton mode:

    public class singleton

    Isn't it strange to define an instance of yourself within yourself?

    Note that this is private and is only available for internal calls.

    private static singleton instance = new singleton();

    There is a static method for external access to the class, which can be accessed directly.

    public static singleton getinstance()

    Single-state classes can be accessed with use.

    The second form above is lazy initialization, which means that the initial singleton is called for the first time, and it does not need to be regenerated later.

    Notice the synchronized in the lazy initialization form, this synchronized is very important, if there is no synchronized, then it is possible to get multiple singleton instances using getinstance(). There has been a lot of discussion about Singleton for Lazy Initialization involving double-checked locking (DCL), and those interested are interested in further investigation.

    The first form is generally considered to be safer.

  4. Anonymous users2024-02-03

    There are five ways to implement the singleton pattern:

    1. Hungry mode. Thread-safe, invokable and inferred, no delayed loading.

    2. Lazy mode. Thread-safe, inefficient invocation, and delayed loading.

    3. Double detection lock mode. Due to the underlying model of the JVM, there are occasional problems, so it is not recommended to use it.

    4. Static internal categories. Thread-safe, invokable efficiency, and delayed loading.

    5. Enumeration classes. Thread-safe, invokable efficiency, indeferred loading, and prevention of reflection and deserialization of invocations.

  5. Anonymous users2024-02-02

    The singleton pattern is designed to avoid the pattern generated by the same instance being generated repeatedly.

    Instances occupy a large amount of memory space, and multiple variables point to the same object, which can save memory.

    The core of the singleton pattern:

    Privatization of the construction method. Privatized cloning methods. Static variables for privatization. Static methods for the public. Reasons for setting up three private and one public:

    The privatized construction method only allows the use of a set public method, and the Jiandan is a vertical instance. The privatization of the Kezon method is to avoid using the public method to get an instance and then clone a new object by cloning it. Privatized static variables are used to store instanced objects of the class.

    A public static method is used to store instantiated objects for external access.

  6. Anonymous users2024-02-01

    The singleton pattern is a common software design pattern. At its core, it contains only one special class called a singleton.

    Extension: 1. The main points of the singleton pattern: first, there can only be one instance of a certain class; The second is that it has to create the instance on its own; The third is that it has to provide this instance to the entire system on its own.

    2. Advantages and disadvantages of singleton mode:

    Advantages: 1. Instance control 2. Flexibility.

    Disadvantages: 1. Overhead 2. Possible development confusion 3. Object lifetime.

    3. The singleton pattern is one of the simplest forms of design patterns. The purpose of this pattern is to make an object of a class the only instance in the system. To achieve this, you can start by instantiating it on the client.

    Therefore, there is a need for a mechanism that allows only the only instance of the object class to be generated, and "blocks" access to all objects that want to be generated. Use the factory method to limit the instantiation process. This method should be static (class method), because it doesn't make sense to have an instance of the class spawn another unique instance.

  7. Anonymous users2024-01-31

    The singleton pattern is arguably the simplest of the GOF design patterns, and it is also the one that bears the most stigma. But I've been unimpressed by a lot of similar comments about it being the worst and anti-pattern.

    The pattern itself is simple, except for a bit of fuzz about multithreaded security issues. So, I don't really want to discuss the reference implementation of the pattern provided by gof. What I'm concerned about is the way of thinking and thinking about the issues that come with it behind it.

    Let's first look at the background of the emergence of the singleton pattern. The singleton pattern is all about making sure that there is only one instance of the class. Okay, so let's ask, why make sure that the class has only one instance?

    In fact, it is nothing more than two aspects: first, if you want to control the use of resources, it is reflected in two aspects, one of which is to control the generation of instances to save resources, and the other is to control the concurrent access of resources through thread synchronization; Second, it is intended to be used as a communication medium to allow two unrelated programs to communicate without establishing a direct relationship, especially multi-threading. The second point, I'm talking about something abstract, but to put it simply, it's like people discussing things on a blackboard without caring about who is involved in the discussion.

    The singleton takes the role of a blackboard.

  8. Anonymous users2024-01-30

    Kobe Bryant is still James good.

Related questions
2 answers2024-03-14

Writing ideas: To tell a story well, grasp the six elements: time, place, people, reasons, process, and results, on this basis, the story can be told completely and thoroughly, and then the first sense of expression can be made, so that the article can be completed in one go, so that readers can resonate. >>>More

3 answers2024-03-14

If you're gone, I'll love you in tears.

If I'm gone, I'll love you from afar. >>>More

10 answers2024-03-14

You definitely don't have any merits, specialties, or hobbies. Just blow it. >>>More

10 answers2024-03-14

Writing modern poetry can imitate the modern poems well written by others like writing ancient poems, borrowing its tone, borrowing its linguistic rhythm, borrowing its rhetorical devices, and borrowing the wisdom of others to enrich one's own works. >>>More

5 answers2024-03-14

Basically, it can be understood that the left is radical and the right is conservative and backward. This kind of thing is all talking about each other and attacking each other. There are no obvious criteria for differentiation and judgment.