温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用Java单例模式

发布时间:2021-11-17 11:34:05 来源:亿速云 阅读:108 作者:iii 栏目:大数据

本篇内容主要讲解“怎么使用Java单例模式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Java单例模式”吧!

怎么使用Java单例模式

  • 1 饿汉式(静态变量)

package com.shi.design.singleton;

/**
 * 单例模式:1 饿汉式(静态变量)
 * @author shiye
 *
 */
public class Singleton1 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = " + singleton1.hashCode());
		System.out.println("singleton2.hashCode() = " + singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  2. 本类内部创建对象实例
 *  3. 提供一个共有的静态方法,返回实力对象
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static final Singleton instance = new Singleton();
	
	public static Singleton getInstance() {
		return instance;
	}
}

怎么使用Java单例模式

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

package com.shi.design.singleton.type2;

/**
 * 单例模式:2 饿汉式(静态代码块)
 * @author shiye
 *
 */
public class Singleton2 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = " + singleton1.hashCode());
		System.out.println("singleton2.hashCode() = " + singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  2. 再静态代码块中创建对象实例
 *  3. 提供一个共有的静态方法,返回实力对象
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static  Singleton instance;
	
	static {
		instance= new Singleton();
	}
	
	public static Singleton getInstance() {
		return instance;
	}
}

怎么使用Java单例模式

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

package com.shi.design.singleton.type3;

/**
 * 单例模式:3 懒汉式式(线程不安全)
 * @author shiye
 *
 */
public class Singleton3 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = " + singleton1.hashCode());
		System.out.println("singleton2.hashCode() = " + singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  23. 提供一个共有的静态方法,再需要的时候去创建找个对象
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static  Singleton instance;
	
	public static Singleton getInstance() {
		if(instance == null) {
			instance= new Singleton();
		}
		return instance;
	}
}

怎么使用Java单例模式

  • 4 懒汉式式(线程安全)

package com.shi.design.singleton.type4;

/**
 * 单例模式:4 懒汉式式(线程安全)
 * @author shiye
 *
 */
public class Singleton4 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = " + singleton1.hashCode());
		System.out.println("singleton2.hashCode() = " + singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  23. 提供一个共有的静态方法,再需要的时候去创建找个对象 (加锁synchronized 解决线程安全的问题)
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static  Singleton instance;
	
	public static synchronized Singleton getInstance() {
		if(instance == null) {
			instance= new Singleton();
		}
		return instance;
	}
}

怎么使用Java单例模式

  • 5 懒汉式式(线程安全,双重检查 -推荐使用)

package com.shi.design.singleton.type5;

/**
 * 单例模式:5 懒汉式式(线程安全,双重检查)
 * @author shiye
 *
 */
public class Singleton5 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = " + singleton1.hashCode());
		System.out.println("singleton2.hashCode() = " + singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  23. 提供一个共有的静态方法,再需要的时候去创建找个对象
 * (加线程代码块synchronized 解决线程安全的问题,并且进行双重检查)
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	//volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。
	private static volatile Singleton instance;
	
	public static Singleton getInstance() {
		if(instance == null) {
			synchronized (Singleton.class) {
				if(instance == null) {
					instance= new Singleton();
				}
			}
		}
		return instance;
	}
}

怎么使用Java单例模式

为什么要加 volatile  主要是为了禁止指令重排

怎么使用Java单例模式

  • 6 懒加载式(静态内部类  推荐使用)

package com.shi.design.singleton.type6;

/**
 * 单例模式:6 懒加载式(静态内部类)
 * @author shiye
 *
 */
public class Singleton6 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = " + singleton1.hashCode());
		System.out.println("singleton2.hashCode() = " + singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  2. 创建一个静态内部类(Singleton 类装载到内存中不会装载SingletonInstance类)
 *  3. 提供一个getInstance()方法:
 *  当getInstance()方法被调用的时候才回去实例化SingletonInstance类(装载类是线程安全的)
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	public static class SingletonInstance{
		public static final Singleton INSTANCE = new Singleton();
	}
	
	public static Singleton getInstance() {
		return SingletonInstance.INSTANCE;
	}
}

怎么使用Java单例模式

  • 7 使用枚举 (推荐使用)

package com.shi.design.singleton.type7;

/**
 * 单例模式:7 使用枚举
 * @author shiye
 *
 */
public class Singleton7 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.INSTANCE;
		Singleton singleton2 = Singleton.INSTANCE;
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = " + singleton1.hashCode());
		System.out.println("singleton2.hashCode() = " + singleton2.hashCode());
		
		singleton1.say();
		singleton2.say();
	}
}

/**
 *  使用枚举
 * @author shiye
 *
 */
enum Singleton{
	INSTANCE;
	public void say() {
		System.out.println("hello ~ ");
	}
}

怎么使用Java单例模式

怎么使用Java单例模式

怎么使用Java单例模式

到此,相信大家对“怎么使用Java单例模式”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI