单例设计模式八种方式
- 恶汉式(静态常量)(推荐使用)
- 构造器私有化(防止new)
- 累的内部创建对象
- 向外暴露一个静态的公共方法。
- 代码实现
package com.lianxi; public class SingletonTest { public static void main(String[] args) { //测试 Singleton instance1 = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance1 == instance2); //true System.out.println("instance1" + instance1.hashCode());//instance1460141958 System.out.println("instance1" + instance2.hashCode());//instance1460141958 } } class Singleton{ //1、构造器私有化(防止new) private Singleton (){ } //2、类的内部创建对象 private final static Singleton instance = new Singleton(); //向外暴露一个静态的公共方法(提供一个共有的静态方法,返回实例对象) public static Singleton getInstance(){ return instance; } }
- 优缺点
- 饿汉式(静态代码块)(推荐使用)
- 代码
package com.lianxi; public class SingletonTest02 { public static void main(String[] args) { //测试 Singleton02 instance1 = Singleton02.getInstance(); Singleton02 instance2 = Singleton02.getInstance(); System.out.println(instance1 == instance2); //true System.out.println("instance1" + instance1.hashCode());//instance1460141958 System.out.println("instance1" + instance2.hashCode());//instance1460141958 } } class Singleton02{ //1、构造器私有化(防止new) private Singleton02 (){ } //2、类的内部创建对象 private static Singleton02 instance02; static { instance02 = new Singleton02(); } //向外暴露一个静态的公共方法(提供一个共有的静态方法,返回实例对象) public static Singleton02 getInstance(){ return instance02; } }
- 优缺点
- 代码
- 懒汉式(线程不安全)
- 代码
package com.lianxi; public class SingletonTest03 { public static void main(String[] args) { //测试 Singleton03 instance1 = Singleton03.getInstance(); Singleton03 instance2 = Singleton03.getInstance(); System.out.println(instance1 == instance2); //true System.out.println("instance1" + instance1.hashCode());//instance1460141958 System.out.println("instance1" + instance2.hashCode());//instance1460141958 } } class Singleton03{ //1、构造器私有化(防止new) private Singleton03 (){ } private static Singleton03 instance03; public static Singleton03 getInstance(){ if(null == instance03){ instance03 = new Singleton03(); } return instance03; } }
- 优缺点
- 代码
- 懒汉式(线程安全, 同步方法)
- 代码
package com.lianxi; public class SingletonTest03 { public static void main(String[] args) { //测试 Singleton03 instance1 = Singleton03.getInstance(); Singleton03 instance2 = Singleton03.getInstance(); System.out.println(instance1 == instance2); //true System.out.println("instance1" + instance1.hashCode());//instance1460141958 System.out.println("instance1" + instance2.hashCode());//instance1460141958 } } class Singleton03{ //1、构造器私有化(防止new) private Singleton03 (){ } private static Singleton03 instance03; /** * 给方法加锁,线程安全了,但是效率低下 * @return */ public static synchronized Singleton03 getInstance(){ if(null == instance03){ instance03 = new Singleton03(); } return instance03; } }
- 优缺点
- 代码
- 懒汉式(线程安全, 同步代码块)
- 代码,将四中方法上的synchronized 放到内部类上
- 优缺点
- 代码,将四中方法上的synchronized 放到内部类上
- 双重检查(推荐使用)
- 代码
- 优缺点
- 代码
- 静态内部类(推荐使用)
- 代码
package com.lianxi; public class SingletonTest05 { public static void main(String[] args) { //测试 Singleton05 instance1 = Singleton05.getInstance(); Singleton05 instance2 = Singleton05.getInstance(); System.out.println(instance1 == instance2); //true System.out.println("instance1" + instance1.hashCode());//instance1460141958 System.out.println("instance1" + instance2.hashCode());//instance1460141958 } } class Singleton05{ private static volatile Singleton05 instance05; //1、构造器私有化(防止new) private Singleton05 (){ } //写一个静态内部类,该类中有一个静态属性singleton private static class SingletonInstance{ private static final Singleton05 INSTANCE = new Singleton05(); } /** * 提供一个静态的共有方法,直接人返回singletonInstance.INSTANCE * @return */ public static Singleton05 getInstance(){ return SingletonInstance.INSTANCE; } }
- 优缺点
- 代码
- 枚举(推荐使用)
- 代码
package com.lianxi; public class SingletonTest06 { public static void main(String[] args) { //测试 Singleton06 instance1 = Singleton06.INSTANCE; Singleton06 instance2 = Singleton06.INSTANCE; System.out.println(instance1 == instance2); //true System.out.println("instance1" + instance1.hashCode());//instance1460141958 System.out.println("instance1" + instance2.hashCode());//instance1460141958 } } enum Singleton06{ INSTANCE; public void sayOk(){ System.out.println("ok---"); } }
- 优缺点
- 代码
单例模式注意事项:
- 注意事项和说明
源码应用:
- Runtime