在进公司java面试时,容易考到的题目,singleton模式的特点并写一个singleton。
单例模式有以下的特点:
1.单例类只可有一个实例。
2.单例类必须自己创建自己这惟一的实例。
3.单例类必须给所有其他对象提供这一实例。
- 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");
- }
- }
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
恶汉式单例类
- 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");
- }
- }
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
懒汉式单例类
- 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");
- }
- }
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
我写的这些仅适用于公司面试,如果要想好好学习设计模式,得全面的看看这方面的资料