IOC concept (DI: dependency injection)

Source: Internet
Author: User
IOC is an inversion of control, that is, the reverse mode. Here is the famous Hollywood Theory: You stay and don't move, I will look for you later. After being renamed as dependency injection dependent injection by Martin Fowler, the relationships between classes are injected through a third party, and the class does not need to resolve the call relationship by itself.
Ainterface A = new ainterfaceimp ();
Ainterfaceimp is a subclass of the ainterface interface. The IOC mode can delay the implementation of the interface. It can be implemented as needed. There is a metaphor: the interface is like an empty model set. When necessary, in order to become a model entity, we need to inject plaster into the model. Therefore, we turn the Implementation of human control interfaces into "injection ".
In fact, the IOC mode also solves the relationship between the caller and the called. The preceding ainterface implementation statement indicates that the called ainterfaceimp is currently called. Because the called name is written into the caller's code, this produces the original sin of an interface implementation: contact each other. The caller and the called are closely related. In UML, dependency is used.

However, this dependency is intolerable in the separation of concerns. It must be cut to decouple callers and callers. The new IOC mode dependency injection mode is generated, the dependency injection mode refers to dependency injection, that is, the dependency is stripped first, and then injected as appropriate.

In traditional cases, in order to achieve the decoupling and separation of callers and callers, the factory mode is generally used. Below we will compare the factory mode and IOC mode to better understand the IOC mode.

Factory mode and IOC

Assume that there are two classes B and C: B as callers, C is called, and there is a call to C in B code:

Public Class B {
Private C comp;
......
}

There are two ways to implement comp instances: single-State factory mode and IOC.

The factory mode is implemented as follows:

Public Class B {
Private C comp;
Private Final Static myfactory = myfactory. getinstance ();

Public B (){
This. Comp = myfactory. createinstanceofc ();

}
Public void somemethod (){
This. Comp. sayhello ();
}
......
}

Features:

  • During each running, myfactory can generate a specific instance of C through createinstanceofc () based on the C subclass defined in the configuration file XML.

Use IOC dependency injection to implement picocontainer as follows. Class B is like a pojo class, as shown below:

Public Class B {
Private C comp;
Public B (C comp ){
This. Comp = comp;
}
Public void somemethod (){
This. Comp. sayhello ();
}
......
}

Assume that the C interface/class has a specific cimp class. Use the following code when the client calls B:

Public class client {
Public static void main (string [] ARGs ){
Defaultpicocontainer Container = new defaultpicocontainer ();
Container. registercomponentimplementation (cimp. Class );
Container. registercomponentimplementation (B. Class );
B = (B) container. getcomponentinstance (B. Class );
B. somemethod ();
}
}

Therefore, when the client calls B, the factory mode and IOC have different features and differences:

The main difference lies in Class B code. If IOC is used, Class B code does not need to be embedded in any factory code, because these factory models are indirectly related to C, IOC is used to completely decouple the relationship between B and C.

The cost of using IOC is that it is necessary to assemble the relationship between B and C on the client or somewhere else.

Therefore, IOC does not eliminate the relationship between B and C, but only transfers the relationship.
This kind of link transfer is actually a kind of separation concern, which has a huge impact and provides the possibility of Implementing AOP.

IOC and AOP

We already know that AOP is a cross-section programming method. Because IOC frees Class B and can inject Class C implementations into Class B, if you think of Class B as a horizontal action at runtime, injection of class C subclass is undoubtedly an advice in AOP, such:

The following code describes how to use picocontainer to implement AOP. This routine is mainly used to record the logger function. Through picocontainer, you can use a simple line to activate the record function of all application classes.

First, compile a record interface:

Public interface Logging {

Public void enablelogging (log );

}

There is a logswitcher class which is mainly used to activate the record function in a specific application:

Import org. Apache. commons. Logging. log;
Public class logswitcher
{
Protected log m_log;
Public void enablelogging (log ){
M_log = log;
M_log.info ("logging enabled ");
}
}

This class can be inherited from common application JavaBeans. If picousermanager is a user management class, the Code is as follows:

Public class picousermanager extends logswitcher
{

... // User management function
}
Public class picoxxxx1manager extends logswitcher
{

... // Business functions
}
Public class picoxxxx2manager extends logswitcher
{

... // Business functions
}

Note that the log instance in logswitcher is assigned by the outside world, that is, it is about to be injected into the external world. Let's take a look at how the picocontainer injects the specific log instance.

Defaultpicocontainer Container = new defaultpicocontainer ();
Container. registercomponentimplementation (picousermanager. Class );
Container. registercomponentimplementation (picoxxxx1manager. Class );
Container. registercomponentimplementation (picoxxxx2manager. Class );
.....

Logging logging = (logging) container. getcomponentmulticaster ();

Logging. enablelogging (New simplelog ("Pico"); // activate log

The code above shows that the record function of all application classes is activated by using a simple line of logging. enablelogging. Is this similar to the advice Implementation of AOP?

In short, the IOC mode can be used to describe the technical architecture at an abstract level regardless of future implementation. Therefore, the IOC mode can provide specific implementation methods for software implementation such as containers and frameworks. It is an important mode application in architecture technology. The J-channel jdonsd framework also uses the IOC mode.

 

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.