IOC principle Analysis

Source: Internet
Author: User
Tags ip number

IOC (inversion of control) controlled inversion

In our program, in order to implement a function, we will use two or more than two classes to work together, then in a class, we will have its cooperative class reference, that is, the class depends on other classes, the acquisition of this cooperative class, there will be a few different situations

Three ways to rely on access:

Java code
    1. Scenario 1. Build yourself
    2. Class person{
    3. Eat () {
    4. Apple a=new Apple ();
    5. }
    6. }

The first way: in the person's Eat () method to write the fruit to die, from the beginning to create objects,

Cons: 1. The person class must be dependent on the Apple class, and if the Apple class is not completed, the compilation cannot pass

2. Can not be changed, when the person wants to eat another fruit, can not be modified

3. It is difficult to share with other people and can only be used alone

The 4.person class is responsible for Apple's entire life cycle, and two classes are always coupled

Java code
    1. Situation 2 obtained through intermediaries
    2. Class person{
    3. Eat (String name) {
    4. Apple a= (apple) fruitfactory.getinstance ("name");
    5. }}

The second way: 1. Indirectly get the desired object by using the engineering class

By using engineering classes, the effect of the program has really improved, but the problem persists

Cons: 1. The generated code for each subclass is written dead in the factory class, and if you want to change the child class, you must change the method in the factory class

2. Interface-oriented programming, generally use the factory class, usually each interface for a project class, when the project is very large, there will be a lot of factory class

Java code
    1. Case 3. Direct injection
    2. Class person{
    3. Eat (Fruit Fruit) {
    4. Apple implements classes for fruit
    5. }
    6. }

The third way: only need to pass in an out-of-the-box object to the method call, different implementations of different objects can be passed (feel that is simple interface-oriented programming benefits, specific advantages, see the following)

In the system, we can use an external container container to uniformly provision the entire system, the creation and acquisition of objects to the external container, by the external container for each component to provide the required build.

For example:

Create a fruit class object in a container apple,

Passing a fruit object dependent on the person class to the person class

will be so much, then, what is the control of what is reversed?

The way the dependent objects are obtained is reversed.

Other words

The control of how an object obtains the object it depends on is reversed into an external container. objects are dependent upon the creation of an object, provided by an external container responsible for coordinating the relationships between the entities in the entire system.

After understanding the basic principles of the IOC

The rest of the question is: How do you pass a reference to a class that relies on an object? (The way that we pass a reference to a dependent object to a class is called injection.)

Next, we need to study, there are several ways to inject objects into the interior of the class

Three ways to inject:

1. Via Interface injection

This approach requires that our own definition of the build class must implement the container given an interface, and then the container through this interface, for our build class to inject the dependent class

Disadvantage: The container to the formation of the intrusion will be very strong, the implementation of the build can only be used for this container, transplant is not strong

2. Setter Injection

In a container, the dependency of the object is passed to the class by invoking the object's setter () method

3. Constructor injection

By using a constructor, when the class is initialized, the dependency of the incoming object

Know that there are three ways to pass the dependency of a class object into the object in a container, but how do we get the object of this class? How should it be relied on?

Is it also in the container that you simply get different objects through new and then call each other?

If that's the case, then we're just doing some code refactoring based on dependency inversion, and it doesn't really reflect the dynamics of the system.

So how can we best reflect the dynamics of the system? How can you minimize the dependency between two classes to achieve decoupling?

We can give the system an XML configuration file,

In the XML configuration file, set the corresponding property information for each object (that is, the specific dependency of the class)

Then in the system, parse the XML file to get an entity class obj class, the Obj class retains no configuration information for an object

Then, according to the principle of reflection, using the information in the Obj class, dynamic generation configuration corresponding object, and call the object Setter () method, complete the injection of the object,

Because XML is just a text file that meets certain formatting requirements,

So we can change the XML file at any time without modifying the source code

To get any of the types of objects we need, and to inject the object completely.

Make the dependency of the object be carried out, and can make the system to the maximum degree of dynamic, has the extensibility

IOC Core philosophy:

1. Do not create objects in the class and do not directly connect to objects and services in code

2. Describe how the object was created in the configuration file, and the links between the individual components

3. External containers connect these by reflection by parsing the configuration file

The Hollywood Principle:don ' t call Us,we ' LL.

That is, all components are passive, not actively contacting (invoking) external code,

To wait for external code calls--------all components are implemented by the container for initialization and mutual invocation.

Simply put, the relationship between the entire program is controlled by the container: The control of the program is reversed to the container, which is the so-called outward transfer

And in our traditional code, it's directly controlled by the program code.

Finally, use an example of a comparative image to illustrate the role of the IOC:

The so-called IOC, for the spring framework, is the responsibility of spring to control the object's life cycle and the relationship between objects. What does that mean, for example, how do we find a girlfriend? The common situation is that we go everywhere to see where there is a beautiful body and good mm, and then inquire about their interests, QQ number, telephone number, IP number, IQ number ..., find ways to know them, give them what they want, then hey ... The process is complex and profound, and we have to design and face each link ourselves. The same is true of traditional program development, in an object, if you want to use another object, you have to get it (your own new one, or a query from Jndi), after the use of the object will be destroyed (such as connection, etc.), the object will always and other interfaces or classes together.

So how does the IOC do it? It's kind of like finding a girlfriend through a dating agency, introducing a third party between me and my girlfriend: the Marriage Institute. Matchmaking management of a lot of men and women's information, I can give a list of matchmaking, tell it I want to find a girlfriend, such as like Michelle Reis, figure like Lin Xire, singing like Jay Chou, speed like Carlos, technology like Zidane, and then the matchmaking will be according to our requirements, provide a mm, We just have to go to love her and get married. As simple as it is, if a matchmaking person doesn't meet our requirements, we'll throw an exception. The whole process is no longer controlled by myself, but by a similar container-like institution that has a matchmaking system.

This is how spring advocates for development, and all classes are registered in the spring container, telling spring what you are, what you need, and then spring will give you what you want when the system runs to the right time, as well as handing you over to other things that need you. All classes are created and destroyed by spring, which means that the object that controls the lifetime of the object is no longer a reference to it, but spring. For a specific object, it was previously controlled by other objects, and now all objects are controlled by spring, so this is called control inversion.

One of the main points of the IOC is to dynamically provide the other objects it needs to an object during the system's operation. This is achieved through DI (Dependency injection, Dependency injection). For example, object A needs to manipulate the database, before we always have to write code in a to get a connection object, with spring we just need to tell spring,a need a connection, as for this connection how to construct, when to construct , a does not need to know. When the system is running, Spring creates a connection at the right time, and then, like an injection, it injects into a, which completes the control of the relationship between the various objects. A relies on connection to function properly, and this connection is injected into a by spring, and the name of the dependency injection comes in. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows the program to dynamically generate objects, execute methods of objects, and change the properties of objects when it is run, and spring is injected through reflection.

Note: Because there are too many articles and blogs, you cannot thank each other. In particular, I would like to thank the It_man Csdn Blog for his great God

And the author of http://www.zhuoda.org/xiaoming/66303.html

and Baidu Encyclopedia author, wikipedia author

As well as Javaeye on the brothers who wrote the IOC

Well, the IOC principle has been tossing the day and finally figuring it out. Then write a toy spring.

IOC principle Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.