On the realization of the principles of the IOC and the IOC framework

A reflection mechanism concept

We consider a scenario, if we run the program, a member of the object you want to view the properties they own, how does it work?

Then consider another scenario, if we want to obtain information about a class of Class at runtime as its properties, constructors, general approach and then consider whether to create its objects, this situation how to do it? That's where the reflection!

We .java file after compilation will become .class file, it's like a mirror itself is .java, .class in the mirror is, in fact, they are the same;

So the same token, we see the reflection in the mirror is .class, will be able to decompile, I learned the true colors .java file.

For the concept of reflection, given the official: Reflection is a feature of the Java language, which allows the program at run time (note not compile time) to carry out self-examination and the members of the internal operation.

For example: It allows a Java class to get all its member variables and methods and displayed.

Primary reflector means can access the program, the ability to detect and modify its own state or behavior, and according to the state of their behavior and results, adjusted or modified as described in the application state and associated behavior semantics.

In Java, just to name a given class, then you can get all the information to the class through reflection.

Java is reflected in a powerful tool that allows us to easily create flexible code, assembly code can run again when, without source code links between components.

But the high reflection incorrectly can cost! What kind of information can be obtained using the reflection of what information, but the premise is the need to know the name of the class.

ROLE reflection mechanism

1. Analyzing any one object belongs to class at runtime;

2. Get the object class at run time;

3. Access java objects at run-time properties, methods, construction method and the like.

We must first figure out why use reflection? Not directly create objects on it yet, which involves the dynamic and static concept.

Statically compiled: determined at compile-time type, binding objects, namely through.

Dynamic compilation: determine the type of running, binding objects . Dynamically compiled Java maximize the flexibility, polymorphism reflects the application, there is to reduce the coupling between classes.

III. Advantages and disadvantages of reflection

Reflection advantage: You can create dynamic objects and compiled, reflecting the great flexibility (especially on the performance of its flexibility is evident in the development of J2EE).

We can get a variety of content classes, decompile through reflection. JAVA language compiler for the first run of this, the reflection mechanism can make the code more flexible and easy to implement object-oriented.

such as:

A large-scale software, it is impossible to put a design perfectly, after this program to compile, publish, and when found necessary to update certain functions, we can not want users to uninstall the previous re-install the new version,

If so, this software is certainly not many people use. Static, then, need to recompile the entire program can be achieved only once the update function, while the use of reflection, then it can not uninstall,

Only need to dynamically create and compile, you can achieve this functionality at runtime.

Disadvantage of reflection: the impact on performance. Using reflection is basically an interpretation operation, we can tell the JVM, what do we want to do and let it meet our requirements. Such operations are always slower than directly perform the same operation.

Technical analysis of four .IOC container

IOC most basic technique is the "reflection (the Reflection)" programming, popular terms is to dynamically generate the object based on the given class name (character string), that allows the programming mode only when the object generated is determined in the end what kind of object.

Just to be produced in Spring object definitions given in the configuration file, the purpose is to improve the flexibility and maintainability.

Currently C #, Java and other languages ​​PHP5 support reflection, which PHP5 technical books, also sometimes translated as "mapping." The concept and usage of reflection, it should be very clear.

Application of reflection is very broad, many mature frameworks, such as Java, Hibernate, Spring Framework, .Net in NHibernate, Spring.NET framework is the "reflection" as the most basic techniques.

Reflective technology actually appeared very early, but has been ignored, not being further utilized. Reflecting the time programming mode with respect to the normal to the object generation slow manner at least 10 times.

Current technology through improvements optimized reflection, is well established, and typically generate an object in reflection object generation mode, the speed has little difference, about 1-2 times the gap.

We can work mode IOC containers seen as a sublimation of the factory model, can be seen as an IOC container factory, this factory to produce objects are defined in the configuration file is given,

Then using the programming language provides the reflection mechanism to form the corresponding object class name given in the configuration file. From the implementation point of view, IOC is put before the factory method to write the dead object code generation,

By changing the configuration file to define, that is, the plant generates both independent and objects separated by, the purpose is to improve the flexibility and maintainability.

Five .IOC alias: dependency injection (DI) 

In 2004, Martin Fowler discusses the same problem, since the IOC is Inversion of Control, then in the end is "what is inverted control of it?", And after the demonstration after a detailed analysis,

He got the answer: "the process of obtaining dependent object is reversed." After the control is reversed, the process of obtaining their dependent objects from the active management becomes injected from the IOC container.

So he "inversion of control" took a more appropriate name is "dependency injection (Dependency Injection)". His answer, in fact, shows how to achieve the IOC: the injection.

The so-called dependency injection, that is, the IOC container during operation, dynamic dependencies injected into certain objects.

Therefore, dependency injection (DI) and reverse control (IOC) are described from different angles of the same thing, that refers to the introduction of IOC container by way of injection dependency, decoupling between objects.

Some six .IOC container products

IOC container under Sun ONE technology system are: there is a lightweight Spring, Guice, Pico Container, Avalon, HiveMind; heavyweight has the EJB;

There are not light not heavy JBoss, Jdon and so on. Spring Framework as one of the Three Musketeers, medium and small projects in Java development SSH (Struts, Spring, Hibernate) are in use, very mature and widely used,

EJB also used in critical industrial projects, such as certain telecommunications services.

IOC container under the .Net technology system has: Spring.Net, Castle and so on. Spring.Net is a transplant from the Spring Java over the IOC container, Castle Windsor is part of IOC container.

They are lightweight framework, more mature, which Spring.Net has been increasingly used in various projects.

VII. Using an IOC Framework should pay attention to what

Using an IOC Framework products can bring great benefits to our development process, but also fully aware of the shortcomings with IOC framework, be aware of, prevent abuse framework.

1. Software system due to the introduction of third-party IOC container, the step of generating objects become somewhat complicated, it could have been something between the two, but nothing out of a procedure, so we just started using the time frame of the IOC, feel the system becomes less intuitive.

    Therefore, the introduction of a new framework, will increase the cost of learning and training team members met, and after the operation and maintenance, the new entrants have had to make the same body of knowledge.

2. As the object is generated by IOC container reflection mode, a certain loss in operating efficiency. If you want to pursue operational efficiency, it must be weighed against this.

3. IOC framework specific to the product (such as Spring) is concerned, it requires a lot of preparation work, more complicated, for some small projects, the objective is also likely to increase the cost of some of the work.

4.IOC framework maturity of the product itself needs to be evaluated, if a product is introduced IOC framework immature, it will affect the whole project, so this is a hidden risk.

We can roughly conclude that: Some small projects or work product, not suitable for use IOC framework products. In addition, if the lack of knowledge and ability of team members, IOC framework for the lack of deep understanding of the product, and do not rush to introduce.

Finally, with particular emphasis on the efficiency of project or product, it is not suitable for the introduction of products IOC framework, as is the case WEB2.0 website.

 

Guess you like

Origin www.cnblogs.com/ZJOE80/p/12606458.html
Recommended