1. 程式人生 > >【劍指offer】面試題 2. 實現 Singleton模式

【劍指offer】面試題 2. 實現 Singleton模式

模式 試題 枚舉 生成 test hand true 方法 單例模式

面試題 2. 實現 Singleton模式

  • 題目:設計一個類,我們只能生成該類的一個實例。
  • 單例模式:確保一個類只有一個實例,並提供了一個全局訪問點。

Code

  • 1.餓漢模式

    //餓漢模式
    public class Singleton1 {
        //私有化構造方法
        private Singleton1(){}
        private static Singleton1 instance = new Singleton1();
        public static Singleton1 getInstance1(){
            return instance;
        }
    }
  • 2.餓漢模式、變種

    //餓漢模式、變種
    public class Singleton2 {
        //私有化構造方法
        private Singleton2(){}
        private static  Singleton2 instance = null;
        static{
            instance = new Singleton2();
        }
        public static Singleton2 getInstance2(){
            return instance;
        }
    }
  • 3.懶漢、線程不安全

    //懶漢、線程不安全
    public class Singleton3 {
        //私有化構造方法
        private Singleton3(){}
        private static Singleton3 instance = null;
        public static Singleton3 getInstance3(){
            if(instance==null){
                instance = new Singleton3();
            }
            return instance;
        }
    }
  • 4.懶漢、線程安全

    //懶漢、線程安全
    public class Singleton4 {
        //私有化構造方法
        private Singleton4(){}
        private static Singleton4 instance;
        public static synchronized Singleton4 getInstance4(){
            if(instance==null){
                instance = new Singleton4();
            }
            return instance;
        }
    }
  • 5.靜態內部類

    //靜態內部類
    public class Singleton5 {
        //私有化構造方法
        private Singleton5(){}
        //靜態內部類
        private static class SingletonHandler{
            private static final Singleton5 INSTANCE = new Singleton5();
        }
        public static Singleton5 getInstance5(){
            return SingletonHandler.INSTANCE;
        }
    }
  • 6.枚舉

    //枚舉
    public enum Singleton6 {
        INSTANCE;  
        public void whateverMethod() {  
    
        }  
    }
  • 7.雙重校驗鎖

    //雙重校驗鎖
    public class Singleton7 {
        //私有化構造方法
        private Singleton7(){}
        private volatile static Singleton7 instance;
        public static Singleton7 getInstance7(){
            if(instance==null){
                synchronized(Singleton7.class){
                    if(instance==null){
                        instance = new Singleton7();
                    }
                }
            }
            return instance;
        }
    }
  • 測試類

    public class Test {
        public static void main(String[] args) {
            Singleton1 instance1 = Singleton1.getInstance1();
            Singleton1 instance2 = Singleton1.getInstance1();
            System.out.println("單例模式一:"+(instance1==instance2));
    
            Singleton2 instance3 = Singleton2.getInstance2();
            Singleton2 instance4 = Singleton2.getInstance2();
            System.out.println("單例模式二:"+(instance3==instance4));
    
            Singleton3 instance5 = Singleton3.getInstance3();
            Singleton3 instance6 = Singleton3.getInstance3();
            System.out.println("單例模式三:"+(instance5==instance6));
    
            Singleton4 instance7 = Singleton4.getInstance4();
            Singleton4 instance8 = Singleton4.getInstance4();
            System.out.println("單例模式四:"+(instance7==instance8));
    
            Singleton5 instance9 = Singleton5.getInstance5();
            Singleton5 instance10 = Singleton5.getInstance5();
            System.out.println("單例模式五:"+(instance9==instance10));
    
            Singleton6 instanceOne = Singleton6.INSTANCE;
            Singleton6 instanceTwo = Singleton6.INSTANCE;
            System.out.println("單例模式五:"+(instanceOne==instanceTwo));
    
            Singleton7 instance11 = Singleton7.getInstance7();
            Singleton7 instance12 = Singleton7.getInstance7();
            System.out.println("單例模式七:"+(instance11==instance12));
        }
    }

    結果

    單例模式一:true
    單例模式二:true
    單例模式三:true
    單例模式四:true
    單例模式五:true
    單例模式五:true
    單例模式七:true

【劍指offer】面試題 2. 實現 Singleton模式