2个原则:
There should be only one instance allowed for a class
We should allow global point of access to that single instance
1. 使用反射,reflection
-
package Singleton;
-
-
import java.lang.reflect.Constructor;
-
-
-
//using Reflection we can set the private constructor to become accessible
-
//at runtime
-
-
public class SingletonDemo2{
-
//early initialization
-
private static SingletonDemo2 instance = new SingletonDemo2();
-
-
private SingletonDemo2(){
-
System.out.println("Creating .... ");
-
if(instance !=null){
-
throw new RuntimeException("Can't create instance,please use get instance");
-
}
-
}
-
-
public static SingletonDemo2 getInstance(){
-
return instance;
-
}
-
-
-
public static void main(String[] args) throws Exception{
-
SingletonDemo2 s1 = SingletonDemo2.getInstance();
-
SingletonDemo2 s2 = SingletonDemo2.getInstance();
-
-
print("s1", s1);
-
print("s2", s2);
-
-
Class clazz= Class.forName("Singleton.SingletonDemo2");
-
Constructor<SingletonDemo2> ctor = clazz.getDeclaredConstructor();
-
ctor.setAccessible(true);
-
SingletonDemo2 s3 = ctor.newInstance();
-
-
print("s3", s3);
-
}
-
-
static void print(String name, SingletonDemo2 obj){
-
String str;
-
str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
-
System.out.println(str);
-
}
-
-
}
2. 如果clone 对象的话,则throws Exception
-
package Singleton;
-
-
import java.lang.reflect.Constructor;
-
-
//not allow clone
-
public class SingletonDemo3 implements Cloneable{
-
//early initialization
-
private static SingletonDemo3 instance = new SingletonDemo3();
-
-
private SingletonDemo3(){
-
System.out.println("Creating .... ");
-
if(instance !=null){
-
throw new RuntimeException("Can't create instance,please use get instance");
-
}
-
}
-
-
public static SingletonDemo3 getInstance(){
-
return instance;
-
}
-
-
@Override
-
protected Object clone() throws CloneNotSupportedException{
-
if(instance != null) throw new CloneNotSupportedException();
-
return super.clone();
-
}
-
-
public static void main(String[] args) throws Exception{
-
SingletonDemo3 s1 = SingletonDemo3.getInstance();
-
SingletonDemo3 s2 = SingletonDemo3.getInstance();
-
-
print("s1", s1);
-
print("s2", s2);
-
-
SingletonDemo3 s3= (SingletonDemo3)s2.clone();
-
print("s3", s3);
-
}
-
-
static void print(String name, SingletonDemo3 obj){
-
String str;
-
str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
-
System.out.println(str);
-
}
-
-
}
3. 允许序列化
-
package Singleton;
-
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.ObjectInputStream;
-
import java.io.ObjectOutputStream;
-
import java.io.Serializable;
-
import java.lang.reflect.Constructor;
-
-
//not allow clone
-
public class SingletonDemo4 implements Serializable{
-
//early initialization
-
private static SingletonDemo4 instance = new SingletonDemo4();
-
-
private SingletonDemo4(){
-
System.out.println("Creating .... ");
-
if(instance !=null){
-
throw new RuntimeException("Can't create instance,please use get instance");
-
}
-
}
-
-
private Object readResolve(){
-
System.out.println("applying read resolve");
-
return instance;
-
}
-
-
public static SingletonDemo4 getInstance(){
-
return instance;
-
}
-
-
@Override
-
protected Object clone() throws CloneNotSupportedException{
-
if(instance != null) throw new CloneNotSupportedException();
-
return super.clone();
-
}
-
-
public static void main(String[] args) throws Exception{
-
SingletonDemo4 s1 = SingletonDemo4.getInstance();
-
SingletonDemo4 s2 = SingletonDemo4.getInstance();
-
-
print("s1", s1);
-
print("s2", s2);
-
-
ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("s2.txt"));
-
oos.writeObject(s2);
-
-
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("s2.txt"));
-
SingletonDemo4 s3= (SingletonDemo4)ois.readObject();
-
print("s3", s3);
-
}
-
-
static void print(String name, SingletonDemo4 obj){
-
String str;
-
str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
-
System.out.println(str);
-
}
-
-
}
4. 如果允许多线程
-
package Singleton;
-
-
import java.lang.reflect.Constructor;
-
-
public class SingletonDemo implements Cloneable{
-
//lazy initialization
-
private static SingletonDemo instance =null;
-
-
private SingletonDemo(){
-
System.out.println("Creating .... ");
-
}
-
-
//in multithreaded environment
-
public static synchronized SingletonDemo getInstance(){
-
//public static SingletonDemo getInstance(){
-
if(instance == null){
-
instance = new SingletonDemo();
-
}
-
return instance;
-
}
-
-
@Override
-
protected Object clone() throws CloneNotSupportedException{
-
//if(instance != null) throw new CloneNotSupportedException();
-
return super.clone();
-
}
-
-
private Object readResolve(){
-
System.out.println("applying read resolve....");
-
return instance;
-
}
-
-
public static void main(String[] args) throws Exception{
-
SingletonDemo s1 = SingletonDemo.getInstance();
-
SingletonDemo s2 = SingletonDemo.getInstance();
-
-
print("s1", s1);
-
print("s2", s2);
-
-
SingletonDemo s3 = (SingletonDemo)s2.clone();
-
-
print("s3", s3);
-
}
-
-
static void print(String name, SingletonDemo obj){
-
String str;
-
str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
-
System.out.println(str);
-
}
-
-
}
阅读(554) | 评论(0) | 转发(0) |