A little simple understanding of Java reflection

After only recently started learning Java, this two-day contact with the frame are very confused about the concept of reflection, a lot of information in the online search found that most of them are talking about how to use reflection. The principle of reflection and why reflection is relatively small, even though there are a lot of stresses are relatively obscure.

Reference: Learn how to understand java reflection? - Luo then do not write code to answer - know almost )

Later, finally found an answer to know almost speak of the role of reflection, I think put it very well, with some slight personal opinion recorded.

Simple fruit plants

The master A used a simple example to illustrate the mode of action of plant reflection:

// Fruit接口
public interface Fruit {
    public void eat();
}

// Apple类
public class Apple implements Fruit{
    public void eat() {
        System.out.println("eat apple.");
    }
}

// Factory 用于生产Fruit
public class Factory {
    public static Fruit getInstance(String className){
        if(className.equals("Apple")){
            return new Apple();
        }else{
            return null;
        }
    }
}

// demo
Fruit f = Factory.getInstance("Apple");
f.eat();

This is a simple example of factory pattern, factory by the judge to decide what kind of parameters passed in fruit production. Here I draw a very simple schematic diagram to illustrate:

Sketch mode

Code and schematic view of the combination of: demo uses strings coming demand, the plant used to determine if conditions demand, using the new object to produce fruit. And judging section and there is a serious demand coupling: any change requests are required to determine changes accordingly . I kind of fruit every increase in demand, it is much more a conditional branch in the judgment.

Reflected use

This code is very simple, so modify the code in a single class is not troublesome. If we produce fruits of different types of plants have one hundred, one thousand, if there are other plants (producing fruit juice) and fruit also need to pass the relevant requirements, it can not be completely resolved. The reflection will be able to solve this problem:

// 重写Factory的代码
public class Factory {
    public static Fruit getInstance(String className){
//        if(className.equals("Apple")){
//            return new Apple();
//        }else{
//            return null;
//        }
        Fruit f = null;
        try {
            f = (Fruit) Class.forName(className).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }
}

We use direct access to the string passed through the reflection type corresponding to the object class, and create a corresponding use of fruit entity class object, thereby eliminating the need to modify the code determination section repeatedly in demand increases. (This should be called decoupling it), also with a diagram to explain:

Reflection analysis

Partial replacement of the original judgment became reflective, that is to say:

  1. No longer needs determined by the plant
  2. Demand is passed to the JVM through direct reflection to find class

Popular explanation

Before the code reflection is not used, we can imagine a factory into a special order administrator, the administrator orders received after each Party will demand their own hands with the production of the form (judgment code) to compare, If the form this one there so the administrator will go to the production line required to start production on (JVM).

After receiving the administrator's command line, they start preparing the production (the class loader related content), if the increase in new demand, so production form also needs to be updated synchronization, very troublesome. And many factories, new requirements are updated every time a form is very difficult.

Thus, the administrator directly laid off (cancel determination section), and a direct connection from the party line, the production line needs direct reference, the production line for production according to the demand:

Have graph LR id1 {} production line - at -> id2 [Production]; id1-- None -> id3 [lookup production]; id3-- Data -> id4 [open line]; id4-- > id2; id3-- no data -> give up;

This figure relates to the more elementary class loader concept, the class loader will be able to look at this picture and the actual class loaded up correspondence.

Guess you like

Origin www.cnblogs.com/acct-zcw/p/12299273.html