-
If you already know what I am going to write about when you see the title, it seems that your skills are still better than mine; if you are confused, just read down carefully.
Let me first write a singleton pattern:
view plaincopy to clipboardprint?
package test.reflect;
public class Singleton {
private static Singleton s= null;
private Singleton() {
}
public static Singleton getInstance() {
if (s == null) {
synchronized (Singleton.class) {
if (s == null) {
s = new Singleton();
}
}
}
return s;
}
}
package test.reflect;
public class Singleton {
private static Singleton s= null;
private Singleton() {
}
public static Singleton getInstance() {
if (s == null) {
synchronized (Singleton.class) {
if (s == null) {
s = new Singleton();
}
}
}
return s;
}
}
This Singleton class is a single instance model, and its construction method is privately modified. External classes cannot create its instance through new. They can only obtain the instance by calling its static method getIntance, and return it in multiple calls. One instance, never create redundant instances.
Our client is as follows:
view plaincopy to clipboardprint?
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
//singleton = new Singleton();This doesn't work
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
//singleton = new Singleton();This doesn't work
The print result is as follows:
view plaincopy to clipboardprint?
test.reflect.Singleton@c17164
test.reflect.Singleton@c17164
But how do we create an instance through the private constructor? The answer is reflection.
Reflective Java is an important module. Understanding reflection can do a lot of work for us. The basic principle of reflection is to map the bytecode of a class into a Class object that can describe various information about the class.
Below we will add some code on the client side:
view plaincopy to clipboardprint?
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
//singleton = new Singleton();This doesn't work
Class<?> clazz = Singleton.class;
//Class<?> clazz = Class.forName("test.reflect.Singleton");//This also works
Constructor<?>[] constructors = clazz.getDeclaredConstructors();//Get the declared constructor
Constructor<?> privateConstructor = constructors[0];//Singleton class has only one constructor
privateConstructor.setAccessible(true);//Set accessible to true to operate it
Singleton instance = (Singleton) privateConstructor.newInstance();
System.out.println(instance);
System.out.println(singleton == instance);
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
//singleton = new Singleton();This doesn't work
Class<?> clazz = Singleton.class;
//Class<?> clazz = Class.forName("test.reflect.Singleton");//This also works
Constructor<?>[] constructors = clazz.getDeclaredConstructors();//Get the declared constructor
Constructor<?> privateConstructor = constructors[0];//Singleton class has only one constructor
privateConstructor.setAccessible(true);//Set accessible to true to operate it
Singleton instance = (Singleton) privateConstructor.newInstance();
System.out.println(instance);
System.out.println(singleton == instance);
The printed result is:
view plaincopy to clipboardprint?
test.reflect.Singleton@c17164
test.reflect.Singleton@1fb8ee3
false
test.reflect.Singleton@c17164
test.reflect.Singleton@1fb8ee3
false
We see that the objects created twice are different. We did create an instance of the Singleton class through the private constructor method.
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/liuhe688/archive/2009/12/31/5110637.aspx
-