在JAVA中如何实现单例模式

单例模式在java中应用很多特别是在学生管理系统等,主要应用与管理员用户等,本实例中有三种方式进行创建单例模式 ,分别是懒汉模式,和饿汉模式
以及使用枚举进行单个用户的创建。

懒汉模式

package 常用类;

public class text1 {//懒汉模式
     private  String name;
     private int sex;
     private static text1 text1=null;
     private text1(String name, int sex) {
		super();
		this.name = name;
		this.sex = sex;	
	}
    public static text1 gettext1() {
    	if (text1==null) {
    		
          text1=new text1("张三",18);
    	}
    	return text1;
    }
	public String getName() {
		return name;
	}
	
	public int getSex() {
		return sex;
	}
	
	public static text1 getText1() {
		return text1;
	}
}

饿汉模式

package 常用类;

public class tear2 {

	String name="";
	int sex;
	private static tear2 sexe=new tear2("张三", 13);
	private tear2(String name, int sex) {
		this.name = name;
		this.sex = sex;
	}
	public String getName() {
		return name;
	}
	
	public int getSex() {
		return sex;
	}
	public static tear2 gettear2() {
		return sexe;
	
}
	}

枚举方式

package 常用类;

public enum test3 {
	stu1(13,"张三");//枚举模式
	private  int sex;
	private String name;
	private test3(int sex, String name) {
		this.sex = sex;
		this.name = name;
	}
	
	

	
	
	
}

main方法进行测试

package test;

import 常用类.tear2;
import 常用类.test3;
import 常用类.text1;

public class main {

	public static void main(String[] args) throws CloneNotSupportedException {
		//测试懒汉模式
	    text1 f1=text1.gettext1();
	    text1 f2=text1.gettext1();
	    if(f1==f2) {
	    	System.out.println("他们是一个对象");
	    }else {
	    	System.out.println("他们不是一个对象");
	    }
	    //测试饿汉模式
	     tear2 g1=tear2.gettear2();
	     tear2 g2=tear2.gettear2();
	     if(g1==g2) {
	    	 System.out.println("他们是一个对象");
	     }else
	     {
	    	 System.out.println("他们不是一个对象");
	     }
	     
	    test3 stu1=test3.stu1;
	    test3 stu2=test3.stu1;	
	    
	    if(stu1==stu2) {
	    	System.out.println("他们是一个对象");
	    }
	    
	   
	}

}

在java中懒汉模式与饿汉模式各有优缺。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java单例模式有以下几种实现方式: 1. 饿汉式单例模式 这种方式在类加载时就创建了单例对象,因此也称为静态单例模式。由于在类加载时就已经创建了单例对象,所以不存在线程安全问题。但是单例对象在整个应用程序生命周期内都存在,可能会降低应用程序的性能。 ```java public class Singleton{ //在类加载时就创建单例对象 private static Singleton instance = new Singleton(); //私有构造函数,防止外部实例化 private Singleton(){} //提供公有的访问方法 public static Singleton getInstance(){ return instance; } } ``` 2. 懒汉式单例模式 这种方式延迟了单例对象的实例化,因此也称为懒汉式单例模式。在多线程环境下,可能会存在线程不安全的情况。 ```java public class Singleton{ //延迟创建单例对象 private static Singleton instance = null; //私有构造函数,防止外部实例化 private Singleton(){} //提供公有的访问方法,采用双重检查锁定保证线程安全 public static Singleton getInstance(){ if(instance == null){ synchronized(Singleton.class){ if(instance == null){ instance = new Singleton(); } } } return instance; } } ``` 3. 静态内部类单例模式 这种方式采用静态内部类来实现单例对象的延迟创建,既可以保证线程安全,又可以实现懒汉式实例化方式。 ```java public class Singleton{ //私有构造函数,防止外部实例化 private Singleton(){} //静态内部类,保证线程安全和懒加载 private static class SingletonHolder{ public static final Singleton instance = new Singleton(); } //提供公有的访问方法 public static Singleton getInstance(){ return SingletonHolder.instance; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值