java面试——单例模式

在进公司java面试时,容易考到的题目,singleton模式的特点并写一个singleton。

单例模式有以下的特点:
1.单例类只可有一个实例。
2.单例类必须自己创建自己这惟一的实例。
3.单例类必须给所有其他对象提供这一实例。

Java代码 复制代码
  1. package com.xyq.demo;   
  2.   
  3. public class Singleton {   
  4.     private static Singleton singleton;   
  5.     private Singleton() {   
  6.     };   
  7.     public static Singleton getInstance() {   
  8.         if (singleton == null)   
  9.             singleton = new Singleton();   
  10.         return singleton;   
  11.     }   
  12. }   
  13.   
  14. // 测试类   
  15. class singletonTest {   
  16.     public static void main(String[] args) {   
  17.         Singleton s1 = Singleton.getInstance();   
  18.         Singleton s2 = Singleton.getInstance();   
  19.         if (s1 == s2)   
  20.             System.out.println("s1 is the same instance with s2");   
  21.         else  
  22.             System.out.println("s1 is not the same instance with s2");   
  23.     }   
  24. }  
package com.xyq.demo;

public class Singleton {
	private static Singleton singleton;
	private Singleton() {
	};
	public static Singleton getInstance() {
		if (singleton == null)
			singleton = new Singleton();
		return singleton;
	}
}

// 测试类
class singletonTest {
	public static void main(String[] args) {
		Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();
		if (s1 == s2)
			System.out.println("s1 is the same instance with s2");
		else
			System.out.println("s1 is not the same instance with s2");
	}
}

 输出:  s1 is the same instance with s2

 

恶汉式单例类

Java代码 复制代码
  1. package com.xyq.demo;   
  2.   
  3. public class Singleton {   
  4.     private static final Singleton instance = new Singleton();   
  5.     private Singleton() {   
  6.     }   
  7.     public static Singleton getInstance() {   
  8.         return instance;   
  9.     }   
  10. }   
  11.   
  12. // 测试类   
  13. class singletonTest {   
  14.     public static void main(String[] args) {   
  15.         Singleton s1 = Singleton.getInstance();   
  16.         Singleton s2 = Singleton.getInstance();   
  17.         if (s1 == s2)   
  18.             System.out.println("s1 is the same instance with s2");   
  19.         else  
  20.             System.out.println("s1 is not the same instance with s2");   
  21.     }   
  22. }  
package com.xyq.demo;

public class Singleton {
	private static final Singleton instance = new Singleton();
	private Singleton() {
	}
	public static Singleton getInstance() {
		return instance;
	}
}

// 测试类
class singletonTest {
	public static void main(String[] args) {
		Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();
		if (s1 == s2)
			System.out.println("s1 is the same instance with s2");
		else
			System.out.println("s1 is not the same instance with s2");
	}
}

 输出:  s1 is the same instance with s2

 

懒汉式单例类

Java代码 复制代码
  1. package com.xyq.demo;   
  2.   
  3. public class Singleton {   
  4.     private static Singleton instance = null;   
  5.   
  6.     /**  
  7.      * 私有的默认构造子,保证外界无法直接实例化  
  8.      */  
  9.     private Singleton() {   
  10.     }   
  11.   
  12.     /**  
  13.      * 静态工厂方法,返还此类的惟一实例  
  14.      */  
  15.     synchronized public static Singleton getInstance() {   
  16.         if (instance == null) {   
  17.             instance = new Singleton();   
  18.         }   
  19.         return instance;   
  20.     }   
  21. }   
  22.   
  23. // 测试类   
  24. class singletonTest {   
  25.     public static void main(String[] args) {   
  26.         Singleton s1 = Singleton.getInstance();   
  27.         Singleton s2 = Singleton.getInstance();   
  28.         if (s1 == s2)   
  29.             System.out.println("s1 is the same instance with s2");   
  30.         else  
  31.             System.out.println("s1 is not the same instance with s2");   
  32.     }   
  33. }  
package com.xyq.demo;

public class Singleton {
	private static Singleton instance = null;

	/**
	 * 私有的默认构造子,保证外界无法直接实例化
	 */
	private Singleton() {
	}

	/**
	 * 静态工厂方法,返还此类的惟一实例
	 */
	synchronized public static Singleton getInstance() {
		if (instance == null) {
			instance = new Singleton();
		}
		return instance;
	}
}

// 测试类
class singletonTest {
	public static void main(String[] args) {
		Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();
		if (s1 == s2)
			System.out.println("s1 is the same instance with s2");
		else
			System.out.println("s1 is not the same instance with s2");
	}
}

 输出:  s1 is the same instance with s2

我写的这些仅适用于公司面试,如果要想好好学习设计模式,得全面的看看这方面的资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值