专业的JAVA编程教程与资源

网站首页 > java教程 正文

[ Java ] 单例模式 的八种实现方式

temp10 2024-09-22 10:00:46 java教程 9 ℃ 0 评论

单例模式的八种实现方式

一、饿汉式(静态常量)

[ Java ] 单例模式 的八种实现方式

二、饿汉式(静态代码块)

三、懒汉式(线程不安全)

四、懒汉式(线程安全,同步方法)

五、懒汉式

六、双重检查

七、静态内部类

八、枚举

单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。

一、饿汉式(静态常量)


优点:在类装载的时候就完成实例化。避免了线程同步问题。

缺点:在类装载的时候就完成实例化,没有达到Lazy Loading的效果。如果从始至终从未使用过这个实例,则会造成内存的浪费

这种方式基于classloder机制避免了多线程的同步问题,不过,instance在类装载时就实例化,在单例模式中大多数都是调用getInstance方法,但是导致类装载的原因有很多种,因此不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化instance就没有达到lazy loading的效果

代码实现

public class Test {

public static void main(String[] args) {

Singleton singleton = Singleton.getInstance();

Singleton singleton1 = Singleton.getInstance();

System.out.println(singleton.hashCode());

System.out.println(singleton1.hashCode());

}

}

class Singleton{

// 构造器私有化,外部不能 new

private Singleton(){

}

// 本类内部创建对象实例

private final static Singleton instance = new Singleton();

// 公有静态方法,返回对象实例

public static Singleton getInstance(){

return instance;

}

}

二、饿汉式(静态代码块)

class Singleton{

// 构造器私有化,外部不能 new

private Singleton(){}

// 本类内部声明对象实例

private final static Singleton instance;

// 在静态代码块中创建单例对象

static {

instance = new Singleton();

}

// 公有静态方法,返回对象实例

public static Singleton getInstance(){

return instance;

}

}

三、懒汉式(线程不安全)


起到了Lazy Loading的效果,但是只能在单线程下使用。

如果在多线程下,一个线程进入了if (singleton == null)判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。所以在多线程环境下不可使用这种方式

结论:在实际开发中,不能使用这种方式.

代码实现

class Singleton{

// 本类内部声明对象实例

private static Singleton instance;

// 构造器私有化,外部不能 new

private Singleton(){}

// 公有静态方法,返回对象实例

// 当使用该方法时,才去创建 instance

public static Singleton getInstance(){

if (instance == null){

instance = new Singleton();

}

return instance;

}

}

四、懒汉式(线程安全,同步方法)

解决了线程不安全问题

结论:在实际开发中,不推荐使用这种方式

代码实现

class Singleton{

// 本类内部声明对象实例

private static Singleton instance;

// 构造器私有化,外部不能 new

private Singleton(){}

// 公有静态方法,加入同步处理的代码,解决线程安全问题

// 当使用该方法时,才去创建 instance

public static synchronized Singleton getInstance(){

if (instance == null){

instance = new Singleton();

}

return instance;

}

}

五、懒汉式

class Singleton{

// 本类内部声明对象实例

private static Singleton instance;

// 构造器私有化,外部不能 new

private Singleton(){}

public static Singleton getInstance(){

if (instance == null){

synchronized (Singleton.class){

instance = new Singleton();

}

}

return instance;

}

}

六、双重检查

class Singleton{

// 本类内部声明对象实例

private static volatile Singleton instance;

// 构造器私有化,外部不能 new

private Singleton(){}

// 静态公有方法,加入双重检查代码

// 解决线程安全问题,同时解决懒加载问题

public static synchronized Singleton getInstance(){

if (instance == null){

synchronized (Singleton.class){

if(instance == null){

instance = new Singleton();

}

}

}

return instance;

}

}

七、静态内部类


class Singleton {

// 构造器私有化,外部不能 new

private Singleton() {

}

// 静态内部类,该类中有一个静态属性

private static class SingletonInstance {

private static final Singleton INSTANCE = new Singleton();

}

public static Singleton getInstance() {

return SingletonInstance.INSTANCE;

}

/**

* 说明:

* JVM 在装载类时,是线程安全的

* 在装载 Singleton 时,不会同时装载 SingletonInstance

*/

}

八、枚举

public class Test {

public static void main(String[] args) {

Singleton singleton = Singleton.INSTANCE;

Singleton singleton1 = Singleton.INSTANCE;

System.out.println(singleton.hashCode());

System.out.println(singleton1.hashCode());

singleton.say();

}

}

enum Singleton{

INSTANCE;

public void say(){

System.out.println("hello");

}

}

单例模式注意事项和细节说明:

单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统性能。

当想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new

单例模式使用的场景:需要频繁的进行创建和销毁的对象、创建对象时耗时过多或耗费资源过多(即:重量级对象),但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象(比如数据源,session工厂等)


Tags:

猜你喜欢

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表