[Design Mode (c)] Singleton - Singleton There are several written?

Once said to me, "You wrote a program?" I nod slightly, he said, "wrote, ...... I will test you a test. Singleton, what is written?" I thought, begging the same people, but also with test me? Turn your face will go no longer ignore. Kung waited a long time, very earnestly said, " can not write strike? ...... I teach you, remember!

Singleton (Singleton Pattern)

Singleton pattern in Java is one of the easiest design pattern. This model involves a single class that is responsible for creating your own objects, while ensuring that only a single object is created. This class provides the only way to access the object can directly access, no instance of the object class.

Singleton three elements: 1. only one singleton instance. 2. singleton class must create their own unique instance. 3. singleton class must provide this example to all other objects.

Scenarios

1. The protagonist camera perspective in game development to follow, usually only one

2. Production requirements unique serial number for security

3.WEB the counter, not once in the database are refreshed every Riga, single cases of congenital cached

4. Excessive resource object created a need to consume, such as I / O connections to the database and the like

 

Written four singleton

Although the presentation language is typescript, and ultimately to achieve javascript, js but not multi-threaded, regardless of thread safety issues. But I will still be listed in the thread safety of these problems, ts not consider, java, c or want.

Lazy mode

class SingleTest {
    private static instance: SingleTest;
    constructor() {

    }

    public static getInstance(): SingleTest {
        if(this.instance == null) 
            this.instance = new SingleTest();
        return this.instance;
    }
}

This is the most basic implementation, why is it called lazy? Because to wait for someone to call you just instantiate the object, there are enough lazy. The disadvantage is obvious: does not support multi-threading. If you want to ensure thread safety, we need to lock synchronized, of course, will affect the efficiency.

Starving mode

class Singleton {  
    private static instance:Singleton  = new Singleton();  
    constructor (){}  
    public static getInstance(): Singleton {  
    return this.instance;  
    }  
}

Very common pattern . Why is it called a hungry man? Because the class is loaded when you instantiate it, others will not serve you finished, Sigui reborn! Easy to write, but the efficiency will improve. Shortcomings are obvious: it is initialized when the class is loaded, wasting memory, prone to garbage objects.

The following modes for multi-threaded language of design patterns, so do not demonstrate ts.

Double lock detection / double checking lock (DCL, i.e., double-checked locking)

public class Singleton {  
    private volatile static Singleton singleton;  
    private Singleton (){}  
    public static Singleton getSingleton() {  
    if (singleton == null) {  
        synchronized (Singleton.class) {  
        if (singleton == null) {  
            singleton = new Singleton();  
        }  
        }  
    }  
    return singleton;  
    }  
}

In this way double locking mechanism, and in safe multithreading can maintain high performance.

Register-based / static inner classes

public class Singleton {  
    private static class SingletonHolder {  
    private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
    public static final Singleton getInstance() {  
    return SingletonHolder.INSTANCE;  
    }  
}

In this way we can achieve the same effect double check the lock mode, but the implementation is simpler. Initialization of the static field using a delay, this mode should be used instead of double lock detection mode. This embodiment applies only to the static field, the double lock detection mode can be used when a delay required to initialize instance fields.

enumerate

public enum Singleton {  
    INSTANCE;  
    public void whateverMethod() {  
    }  
}

The best way to realize the single mode embodiment. It is more compact, automatic support serialization mechanism to prevent multiple instances of the absolute. But after the enum feature to the need JDK1.5 before use.

 

Kung show a very cheerful, the two fingers are long fingernails tapping the counter, he nodded and said, "Yes Yes! ...... Singleton has written four things, you know?

 

Guess you like

Origin www.cnblogs.com/harrickheng/p/11261465.html