package com.waibizi; /** * Hungry singleton design pattern Han formula (static constant) * Step 1. privatization constructor * (prevent new) * 2. The internal class to create objects * 3. The externally exposed a static public methods getInstance * 4 code implementation * @Author crooked nose * * * Advantages: such an approach is relatively simple, the class loading when instantiation is completed. Avoid thread synchronization problems * Cons: When loading is complete class is instantiated, if the end did not used to from the beginning of the class, will inevitably lead to waste of resources * Conclusion: This model is available in single cases, may lead to waste (time will be calling getInstance class loader, but we can not guarantee that other methods do not lead to class status, class status once occurs, it will certainly take some memory space) */ @SuppressWarnings ( "All" ) public class Singleton_Pattern { // 1. privatization constructor, not outside new new Private Singleton_Pattern () { } // 2. The class object instance is created internally Private Final static Singleton_Pattern instance = new new Singleton_Pattern (); // exposed outwardly 3. a public static method the getInstance public static Singleton_Pattern the getInstance () { return instance; } public static void main(String[] args) { Singleton_Pattern test = Singleton_Pattern.getInstance(); Singleton_Pattern test1= Singleton_Pattern.getInstance(); //hashcode比较 System.out.println(test.hashCode()); System.out.println(test1.hashCode()); } }
Singleton design pattern of starving formula (static constant)
Guess you like
Origin www.cnblogs.com/waibizi/p/12079587.html
Recommended
Ranking