java设计模式(单例模式)

本文详细介绍了Java中的单例模式,包括饿汉式(静态常量、静态代码块)、懒汉式(线程不安全、线程安全的同步方法和同步代码块)、双重检查、静态内部类以及枚举实现方式。每种方式都确保了类只有一个实例,并提供了全局访问点。单例模式能够节省系统资源,提高性能,同时强调了构造器的私有化。
摘要由CSDN通过智能技术生成

设计模式

创建型模式

单例模式

单例模式的八种方式

  1. 饿汗式(静态常量)

  2. 饿汉式(静态代码块)

  3. 懒汉式(线程不安全)

  4. 懒汉式(线程安全,同步方法)

  5. 懒汉式(线程不安全,同步代码块)

  6. 双重检查

  7. 静态内部类

  8. 枚举


饿汉式(静态常量)

public class SingletonTest1 {
	public static void main(String[] args) {
		// 测试
		for (int i = 0; i < 10; i++) {
			new Thread(() -> {
				Singleton singleton = Singleton.getInstence();
				System.out.println("饿汗式(静态变量):" + Thread.currentThread().getName() + ":" + singleton.hashCode());
			}).start();
		}
	}

}

// 饿汗式(静态变量)
class Singleton {

	// 1.构造器私有化,外部不能new
	private Singleton() {

	}
	
	// 2.本类内部创建对象实例
	private final static Singleton instence = new Singleton();
	
	// 3.提供一个公有的静态方法,返回实例对象
	public static Singleton getInstence() {
		return instence;
	}
}

饿汉式(静态代码块)

public class SingletonTest02 {
    public static void main(String[] args) {
    	Singleton02 singleton1 = Singleton02.getInstence();
    	Singleton02 singleton2 = Singleton02.getInstence();
        System.out.println(singleton1 == singleton2); // true
        System.out.println(singleton1.hashCode());
        System.out.println(singleton2.hashCode());
    }

}

// 饿汗式(静态代码块)
class Singleton02{
	
    // 1.构造器私有化,外部不能new
    private Singleton02(){

    }
    
    // 2.本类内部创建对象实例
    private static Singleton02 instence;
    
    // 在静态代码块执行时,创建单例对象
    static {
        instence = new Singleton02();
    }
    
    public static Singleton02 getInstence(){
        return instence;
    }

}

懒汉式(线程不安全)

public class SingletonTest03 {
    public static void main(String[] args) {
    	
    	for (int i = 0; i < 15; i++) {
			new Thread(()->{
				Singleton03 singleton1 = Singleton03.getInstance();
				System.out.println(Thread.currentThread().getName()+":"+singleton1.hashCode());
			}).start();
		}
    	
    }

}

// 懒汉式(线程不安全)
class Singleton03{
	private static Singleton03 singleton;
	private Singleton03(){
		
	}
	
	public static Singleton03 getInstance() {
		if (null == singleton) {
			singleton = new Singleton03();
		}
		return singleton;
	}
}

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

public class SingletonTest04 {
    public static void main(String[] args) {
    	
    	for (int i = 0; i < 15; i++) {
			new Thread(()->{
				Singleton04 singleton1 = Singleton04.getInstance();
				System.out.println(Thread.currentThread().getName()+":"+singleton1.hashCode());
			}).start();
		}
    	
    }

}

// 懒汉式(线程安全,同步方法)
class Singleton04{
	private static Singleton04 singleton;
	private Singleton04(){
		
	}
	
	public static synchronized Singleton04 getInstance() {
		if (null == singleton) {
			singleton = new Singleton04();
		}
		return singleton;
	}
}

懒汉式(线程不安全,同步代码块)

public class SingletonTest05 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton05 singleton1 = Singleton05.getInstance();
				System.out.println(Thread.currentThread().getName() + ":" + singleton1.hashCode());
			}).start();
		}

	}

}

// 懒汉式(线程不安全,同步代码块)
class Singleton05 {
	private static Singleton05 singleton;

	private Singleton05() {

	}

	public static Singleton05 getInstance() {

		if (null == singleton) {
			synchronized (Singleton05.class) {
				singleton = new Singleton05();
			}
		}

		return singleton;
	}
}

双重检查

public class SingletonTest06 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton06 singleton1 = Singleton06.getInstance();
				System.out.println(Thread.currentThread().getName() + ":" + singleton1.hashCode());
			}).start();
		}

	}

}

// 双重检查
class Singleton06 {
	private static Singleton06 singleton;
	
	private Singleton06() {

	}

	public static Singleton06 getInstance() {

		if (singleton == null) {
			synchronized (Singleton06.class) {
				if(singleton == null) {
					singleton = new Singleton06();
				}
			}
		}

		return singleton;
	}
}

静态内部类

public class SingletonTest07 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton07 singleton1 = Singleton07.getInstance();
				System.out.println("静态内部类:"+Thread.currentThread().getName() + ":" + singleton1.hashCode());
			}).start();
		}

	}

}

// 静态内部类
class Singleton07 {
	
	private static class SingletonInstance{
		private static final Singleton07 INSTANCE = new Singleton07();
	}

	private Singleton07() {

	}

	public static Singleton07 getInstance() {
		return SingletonInstance.INSTANCE;
	}
}

枚举

public class SingletonTest08 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton08 instance = Singleton08.INSTANCE;
				instance.sayHello();
				System.out.println("枚类:"+Thread.currentThread().getName() + ":" + instance.hashCode());
			}).start();
		}

	}

}

// 枚举,可以实现单例
enum Singleton08{
	INSTANCE;
	public void sayHello() {
		System.out.println("hello...");
	}
}

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

  1. 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要繁琐创建销毁的对象,使用单例模式可以提供系统性能
  2. 当想实例化一个单例类的时候,必须使用相应的获取对象的方法,而不是使用new
  3. 构造函数是私有的
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值