Java reflection
Reflecting the respective component is a Java class mapped to one Java object, i.e., in the operating state, for any class, so will be able to know the properties and methods of this class; for any one object, it can call any and a method attribute.
Dynamic access to information and the function of dynamic invocation object method called Java's reflection mechanism.
Mainly achieved by the following reflection class (classes are located java.lang.reflect package):
-
Class categories: on behalf of a class. Field class: the field representing the class (also known as member variables of the class attribute).
-
Method class: class method represents.
-
Constructor class: class constructor method represents.
- Array class: provides a dynamically created array and a static method to access the array elements.
Reflection API Class class is a class in the core, the main methods are as follows:
-
getName (): get the full name of the class. getFields (): public class type attribute obtained.
-
getDeclaredFields (): get all the properties of the class.
-
getMethods (): public class type obtained by the method.
-
getDeclaredMethods (): get all methods of the class.
-
getMethod (String name, Class [] parameterTypes): class-specific method to obtain the name of the method name specified parameters, parameter types specified method parameterTypes parameters.
-
getConstrutors (): Constructor public class type obtained.
-
getConstrutor (Class [] parameterTypes): to obtain a particular constructor class parameter type constructor specifies parameterTypes parameter.
-
newInstance (): create an object class by the constructor with no arguments class.
Factory Pattern
Factory mode is divided into three types:
- Simple factory pattern (Simple Factory)
- Factory method model (Factory Method)
- Abstract Factory (Abstract Factory)
Simple Factory:
interface Car { public void Name(); } class Aodi implements Car { public void Name() { System.out.println("Aodi"); } } class Aotuo implements Car { public void Name() { System.out.println("Aotuo"); } } class Factory{ private Factory() {} public static Car getInstance(String className) { if("Aodi".equalsIgnoreCase(className)) { return new Aodi(); }else if("Aotuo".equalsIgnoreCase(className)) { return new Aotuo(); } return null; } } public class Test { public static void main(String[] args) throws Exception { Car car = Factory.getInstance("Aodi"); car.Name(); } }
Introducing reflection, factory pattern:
class Factory2 { private Factory2() {} public static Car getInstance(String className) { Car instance = null; try { instance = (Car) Class.forName(className).getDeclaredConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); } return instance; } } public class Test2 { public static void main(String[] args) throws Exception { Car car = Factory2.getInstance("sample.Aodi"); car.Name(); } }
It can also be used to solve generic, abstract factory:
class Factory3 { private Factory3() {} @SuppressWarnings("unchecked") //instanceName 接口的子类 className接口的类型 public static <T> T getInstance(String instanceName, Class<T> className ) { T instance = null; try { instance = (T) Class.forName(instanceName).getDeclaredConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); } return instance; } } public class Test3 { public static void main(String[] args) throws Exception { Car car = Factory3.getInstance("sample.Aodi", Car.class); car.Name(); } }