脚本宝典收集整理的这篇文章主要介绍了【设计模式从入门到精通】01-单例模式,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)
比如 Hibernate 的 SessionFactory,它充当数据存储源的代理,并负责创建 Session 对象。SessionFactory 并不是轻量级的,一般情况下,一个项目通常只需要一个 SessionFactory 就够,这是就会使用到单例模式
public class Singleton {
// 1、构造器私有化
PRivate Singleton() {
}
// 2、类的内部创建对象
private static final Singleton instance = new Singleton();
// 3、向外暴露一个静态的公共方法
public static Singleton getInstance() {
return instance;
}
}
优缺点
public class Singleton {
// 1、构造器私有化
private Singleton() {
}
// 2、类的内部声明对象
private static Singleton instance;
// 3、在静态代码块中创建对象
static {
instance = new Singleton();
}
// 4、向外暴露一个静态的公共方法
public static Singleton getInstance() {
return instance;
}
}
优缺点
// 1、构造器私有化
private Singleton() {
}
// 2、类的内部声明对象
private static Singleton instance;
// 3、向外暴露一个静态的公共方法,当使用到该方法时,才去创建 instance
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
优缺点
public class Singleton {
// 1、构造器私有化
private Singleton() {
}
// 2、类的内部声明对象
private static Singleton instance;
// 3、向外暴露一个静态的公共方法,加入同步处理的代码,解决线程安全问题
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
优缺点
getlnstance()
方法都要进行同步。而其实这个方法只执行一次实例化代码就够了,后面的想获得该类实例,直接return
就行了。方法进行同步效率太低public class Singleton {
// 1、构造器私有化
private Singleton() {
}
// 2、类的内部声明对象
private static Singleton instance;
// 3、向外暴露一个静态的公共方法,加入同步处理的代码,解决线程安全问题
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
instance = new Singleton();
}
}
return instance;
}
}
优缺点
volatile
关键字修饰修饰public class Singleton {
// 1、构造器私有化
private Singleton() {
}
// 2、类的内部声明对象,同时用`volatile`关键字修饰修饰
private static volatile Singleton instance;
// 3、向外暴露一个静态的公共方法,加入同步处理的代码块,并进行双重判断,解决线程安全问题
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
优缺点
public class Singleton {
// 1、构造器私有化
private Singleton() {
}
// 2、定义一个静态内部类,内部定义当前类的静态属性
private static class SingletonInstance {
private static final Singleton instance = new Singleton();
}
// 3、向外暴露一个静态的公共方法
public static Singleton getInstance() {
return SingletonInstance.instance;
}
}
优缺点
getlnstance
方法,才会装载Singletonlnstance 类,从而完成 Singleton 的实例化public enum Singleton {
INSTANCE;
public void sayHello() {
System.out.println("Hello World");
}
}
优缺点
JDK中 java.lang.Runtime 就是经典的单例模式
虽然上述提到的概念中,将双重检查、静态内部类、枚举三种方式的单例模式单独列举出来说明,但个人觉得本质也可以归类到饿汉式和懒汉式中;另外,同步代码块虽然上述中归类到线程安全,实际上并不是线程安全的
总结如下
分类 | 方式 | 懒加载 | 线程安全 | 效率 | 内存 | 推荐指数(仅供参考) |
---|---|---|---|---|---|---|
饿汉式 | 静态变量 | ❌ | ✔️ | ✔️ | ❌ | ⭐⭐ |
~ | 静态代码块 | ❌ | ✔️ | ✔️ | ❌ | ⭐⭐ |
~ | 枚举 | ❌ | ✔️ | ✔️ | ❌ | ⭐⭐⭐ |
懒汉式 | 线程不安全 | ✔️ | ❌ | ✔️ | ✔️ | ⭐ |
~ | 同步代码块 | ✔️ | ❌ | ✔️ | ✔️ | 不要使用 |
~ | 同步方法 | ✔️ | ✔️ | ❌ | ✔️ | ⭐ |
~ | 双重检查 | ✔️ | ✔️ | ✔️ | ✔️ | ⭐⭐⭐ |
~ | 静态内部类 | ✔️ | ✔️ | ✔️ | ✔️ | ⭐⭐⭐ |
以上是脚本宝典为你收集整理的【设计模式从入门到精通】01-单例模式全部内容,希望文章能够帮你解决【设计模式从入门到精通】01-单例模式所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。