-->
gudangupdate.com
profile

Gudang Update

[Java] Singleton's understanding and complete definition

[Java] Singleton's understanding and complete definition

Java Singleton Understanding And Complete Definition
Singleton is a class that allows only one instance of it self to be created and gives access to the instance created, with an introduction to a single design pattern and general implementation. It contains static variables that can accommodate unique and personal examples them selves. This is used in scenarios when the user wants to limit class instantiation to only one object. This is usually useful when an object is needed to coordinate actions throughout the system.

The singleton pattern is used in programming languages ​​like Java and .NET to define global variables. A single object that is used across systems remains constant and needs to be defined only once and again.

Introduction
The singleton design pattern limits the instantiation of a class into one instance. This is done to provide coordinated access to certain resources, throughout the entire software system. Through this design pattern, the singleton class ensures that it is only used once, and can provide easy access to single instances.

Use Case
The Use-Case that is commonly used for single design patterns including factories, builders, and objects that have program status.

Singletons are sometimes considered as alternatives to global variables or static classes.

Compared to global variables, singletons have the following benefits:

• The Singleton instance field does not take up space in the global namespace

• Singletons may be lazy to be initialized (for further discussion)

Especially because of the fact that the singleton holds the object used, while the static class does not, Singletons have the following advantages compared to static classes:

• Singletons can implement the interface

• Singletons can be sent as parameters

• Singletons can ask for their instance to be exchanged (such as for testing purposes)

• Singletons can be handled polymorphically, so there may be several implementations

Implementation
Let's look at the details of the implementation of the basic singleton class on Java. Singleton is usually implemented with a private constructor method, and the public static method for returning singleton instances - is stored in a private static final variable. There are two types of single implementations: Eager and Lazy initialization. They are different in how to initialize a single instance. Make sure we also have to consider the thread security of the two single implementations.

Eager Initialization
In this version, the singleton instance is created when the singleton variable is initialized, not when it is first used. Why? Because the singleton is not used when the program is running, because singleton can spend system resources unnecessarily. If a single instance is expensive to calculate or consume a lot of resources, this can reduce system performance. However, this version is safe to use.


public final class Singleton {

    private static final Singleton INSTANCE = new Singleton();
    private Singleton() {}
    public static Singleton getInstance() {
            return INSTANCE;
                }
                }
Lazy Inisialisasi
In this version, the singleton instance is created when the getInstance static method is first called. This ensures that a single instance consumes only system resources when absolutely necessary.


public final class Singleton {

    private static Singleton instance = null;
    private Singleton() {}
    public static Singleton getInstance() {
            if (instance == null) {
                        instance = new Singleton();
                                }
                                        return instance;
                                            }
                                            }
Thread-safe Lazy Initialization
The lazy initialization version shown above is not safe for threads. Examples of the singleton can be made several times, in a program with lots of threads, all use the Singleton class simultaneously. If a single object is very expensive to make, this might consume a lot of available system resources. In addition, this can produce threads that receive partially created singleton objects.

The implementation below is safe to use through synchronization. An instance variable is now also expressed as volatile, which ensures that all threads have an updated view of the singleton instance.


public final class Singleton {

    private static volatile Singleton instance = null;
    private Singleton() {}
    public static Singleton getInstance() {
            if (instance == null) {
                        synchronized(Singleton.class) {
                                        if (instance == null) {
                                                            instance = new Singleton();
                                                                            }
                                                                                        }
                                                                                                }
        return instance;
            }
            }
so the discussion this time about, java singleton's understanding and complete definition.

Load comments

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel

CLOSE ADS
CLOSE ADS