设计模式——单例模式

    技术2022-07-20  61

    使用场景

    一个类只需要生成一个实例对象

    关键点

    类中对应的构造方法必须为私有方法,这种其外部就不能调用构造方法来构造对象在类中申明一个类对象,并且用static修饰。

    各种单例模式代码

    注:第一种、第六种、第七种、第八种值得好好看看

    第一种:

    public class singleton01 { private static final singleton01 INSTANCE = new singleton01(); private singleton01(){}; public static singleton01 getInstance(){ return INSTANCE; } public static void main(String[] args) { singleton01 s1 = singleton01.getInstance(); singleton01 s2 = singleton01.getInstance(); System.out.println(s1 == s2); } }

    缺点:

    类装载时就完成了实例化 不能解决反序列化

    优点:

    简单实用 保证线程安全

    第二种:

    public class singleton02 { private static final singleton02 INSTANCE; static { INSTANCE = new singleton02(); } private singleton02(){}; public static singleton02 getInstance(){ return INSTANCE; } public static void main(String[] args) { singleton02 s1 = singleton02.getInstance(); singleton02 s2 = singleton02.getInstance(); System.out.println(s1 == s2); } }

    和第一种差不多。

    第三种:

    public class singleton03 { private static singleton03 INSTANCE; private singleton03(){}; public static singleton03 getInstance(){ if(INSTANCE == null){ try { Thread.sleep(1); }catch (InterruptedException e) { e.printStackTrace(); } INSTANCE = new singleton03(); } return INSTANCE; } public static void main(String[] args) { for(int i = 0;i<100;i++){ new Thread(()-> System.out.println(singleton03.getInstance())).start(); } } }

    缺点:

    线程不安全 不能防止反序列化

    优点

    解决了对象在类加载时就完成实例化的问题

    第四种:

    public class singleton04 { private static singleton04 INSTANCE; private singleton04(){}; public static synchronized singleton04 getInstance(){ if(INSTANCE == null){ try { Thread.sleep(1); }catch (InterruptedException e) { e.printStackTrace(); } INSTANCE = new singleton04(); } return INSTANCE; } public static void main(String[] args) { for(int i = 0;i<100;i++){ new Thread(()-> System.out.println(singleton04.getInstance())).start(); } } }

    缺点:

    这种方式效率比较低 不能防止反序列化

    优点

    解决第三种中的线程不安全问题

    第五种:

    public class singleton05 { private static singleton05 INSTANCE; private singleton05(){}; public static singleton05 getInstance(){ if(INSTANCE == null){ synchronized (singleton05.class){ try { Thread.sleep(10000); }catch (InterruptedException e) { e.printStackTrace(); } INSTANCE = new singleton05(); } } return INSTANCE; } public static void main(String[] args) { for(int i = 0;i<100;i++){ new Thread(()-> System.out.println(singleton05.getInstance())).start(); } } }

    缺点:

    线程不安全 不能防止反序列化

    优点

    减少第五种中的效率问题

    第六种:

    public class singleton06 { private static singleton06 INSTANCE; private singleton06(){}; public static synchronized singleton06 getInstance(){ if(INSTANCE == null){ // 双重检查,解决线程安全问题 synchronized (singleton06.class){ if(INSTANCE == null) { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } INSTANCE = new singleton06(); } } } return INSTANCE; } public static void main(String[] args) { for(int i = 0;i<100;i++){ new Thread(()-> System.out.println(singleton06.getInstance())).start(); } } }

    缺点:

    不能防止反序列化

    优点

    线程安全

    第七种:

    public class singleton07 { private singleton07(){}; private static class singleton07Inner{ private final static singleton07 INSTANCE = new singleton07(); } private static singleton07 getInstance(){ return singleton07Inner.INSTANCE; } public static void main(String[] args) { for(int i = 0;i<100;i++){ new Thread(()-> System.out.println(singleton07.getInstance())).start(); } } }

    缺点:

    不能防止反序列化

    优点

    线程安全

    第八种:

    public enum singleton08 { INSTANCE; public static void main(String[] args) { for(int i = 0;i<100;i++){ new Thread(()-> System.out.println(singleton08.INSTANCE)).start(); } } }

    优点

    线程安全 解决反序列化问题

    Processed: 0.008, SQL: 9