IOC mode (also known as DI: Dependency Injection)

xiaoxiao2021-04-05  261

IOC mode (also known as DI: Dependency Injection)

Separation of Concerns: SoC) is the IOC mode and AOP to produce the most primitive power, and you can get a focus through functional decomposition, which can be component Components, aspect aspects or service services.

From the GOF design mode, we have become accustomed to a thinking programming: Interface Driven Design interface driver, interface driver has a lot of benefits, can provide different flexible subclasses, increased code stability and robustness, but interface must need Implemented, that is, as soon as possible sooner or later:

Ainterface a = new ainterfaceimp ();

AinterfaceImp is a subclass of interface ainterface, and IOC mode can delay the implementation of the interface, as needed, there is a metaphor: the interface is like a model set, when necessary, need to enter the model set to the model entity, so in order to become a model entity, Therefore, we will implement the implementation of artificial control interfaces into "injection".

IOC English is Inversion of Control, ie in reverse mode, here is a famous Hollywood Theory: You stay with you, I will find you.

In fact, IOC mode is also a relationship between the caller and the caller, the above configured statement indicates that the current is called the caller AINTERFACEIMP, and since the caller name is written to the caller code, this generates one The original sins implemented by the interface: contacts each other, the caller and the invigible have a close connection, and it is represented in UML.

However, this dependence is impedably inottable under the thinking of separation, must cut, realize the caller and the modified decoupling, the new IOC mode Dependency INJECTION mode is generated, the Dependency Injection mode is the meaning of the injection, also It is to deepen it first, and then inject it in the appropriate time.

There are three types of IOC mode (Dependency Injection mode):

The first type is a caller from JNDI or ServiceManager, etc., here is similar to ServiceLocator mode. 1. EJB / J2EE2. Avalon (Apache is a complex use of a complex project) second type Using JavaBeans's setter method 1. Spring framework, 2. Webwork / xwork third type Implementation in the constructor 1. PicoContainer , 2. Hivemind

People who have experienced EJB development know that each EJB call needs to find the home-nature of the factory through JNDI. In the chapter of my tutorial EJB, I also explain the use of EJB from the perspective of dependence and plant mode. .

In general, in order to achieve the calorie and the modified decoupling, separation, it is generally implemented by the factory mode, and the following will be different by comparing the factory model and IOC mode, and deepen the understanding IOC mode.

Factory model and IOC

Suppose there are two classes B and C: B as the caller, C is called by the caller, and there is a call to C in the B code:

Public class b {private c concer; ......}

There are two ways to implement the CoMP instance: single plant mode and IOC.

The factory model is achieved as follows:

public class B {private C comp; private final static MyFactory myFactory = MyFactory.getInstance (); public B () {this.comp = myFactory.createInstanceOfC ();} public void someMethod () {this.comp.sayHello (); } ...} Features:

Each time you run, MyFactory can implement C subs class defined in the profile XML and generate C's specific instance of CreateInstanceOfc ().

Using IOC Dependency Injection (Dependency Injection) implements PicoContainer as follows, Class B is like usually Pojo classes, as follows:

Public class b {private c comp; public b (c comp) {this.comp = comp;} public void someMethod () {this.comp.sayhello ();} ...

Suppose there is a specific implementation CIMP class in the C interface / class. When the client calls B, use the following code:

public class client {public static void main (String [] args) {DefaultPicoContainer container = new DefaultPicoContainer (); container.registerComponentImplementation (CImp.class); container.registerComponentImplementation (B.class); B b = (B) container.getComponentInstance (B.class); b.SomeMethod ();}}

Therefore, when the client calls B, there are different features and distins the plant mode and IOC, respectively:

The main difference is reflected in Class B code. If IOC is used, the code will not be embedded in any factory mode in Class B code, because these factory models are actually indirectly linked to C, so that IoC is completely decoupled. The connection between B and C.

The price brought to IOC is: need to be contacted between B and C at the client or other somewhere.

Therefore, IOC does not eliminate such links between B and C, just transferring this connection. This type of contact is actually a separation concern. It has a huge impact, which provides the possibility of AOP implementation.

IOC and AOP

AOP we already know that it is a cut-faced programming method, since IOC is free to free Class B, and can implement an injection C class to the Class C, if you imagine Class B is a horizontal action when it is runtime, there is undoubtedly injecting Class C The class is an advice in the AOP, as shown below:

Through the following code, explain how to use the PicoContainer to implement AOP. This routine is to record the logger function. You can use simple rows through the PicoContainer to activate all application classes.

First prepose a record interface:

Public interface logging {public void enablelogging (log log);

There is a logwitcher class, mainly used to activate the record function in the specific application:

import org.apache.commons.logging.Log; public class LogSwitcher {protected Log m_log; public void enableLogging (Log log) {m_log = log; m_log.info ( "Logging Enabled");}} an ordinary application can JavaBeans Inheriting this class, assuming that PICOUSERMANAGER is a user management class, the code is as follows:

Public Class PicouserManager Extends Logswitcher {..... // User Management Function} Public Class Picoxxx1Manager Extends Logswitcher {..... // Business Function} Public Class PicoxxxX2Manager Extends Logswitcher {..... // Business function}

Note that the log instance in logwitcher is given by the outside world, that is, it is about to be injective to the outside, and look at how to use the PicoContainer how to inject the specific instance of the log.

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

It can be seen from the upper code to activate all application clauses by using simple rows logging.enableelogging (). Is this similar to the AOP Advice implementation?

In summary, use IOC mode, you can perform a description and technical architecture in the future, and therefore, the IOC mode can provide specific implementation means for software implementation of the container, framework, which belongs to the architecture technology. Important mode application. The JDONSD framework of the J channel also uses IOC mode.

Reference:

Inversion of Control Containers and The Dependency Injection Patterna Brief Introduction To IOC

IOC container revolutionary advantages

Java Enterprise System Architecture Selection Consideration

Thinking and doubt about IOC mode

Transfer from: http://www.jdon.com/aOpdesign/ioc.htm

转载请注明原文地址:https://www.9cbs.com/read-132108.html

New Post(0)