Inversion of Control

zhaozj2021-02-16  53

Paul Hammant

Translation: James Shen

Summary

Inversion of Control is a design pattern for solving the dependency, configuration and lifecycle between the module (actually also a simple Java class), where the processing of the module dependency is the Essence of IoC. .

Module dependence

There is the following benefits between the reduction in modules:

Increase the degree of multiplexing

Easy the test of the class easier

Make the entire system easier to assemble and configure

Description

After using IOC mode, we don't need to rely on the dependencies between your own management modules, just declare that this dependency is made by the container to achieve this dependency. It seems that the control of the dependency between the modules is inverted, and this dependency is not established by the module to hand over the container (such as PicoContainer, Spring).

example

Here is a very simple example to illustrate the IOC mode:

Public Interface

Orange

{

//Methods

}

Public class appleImpl imports apple {

Private

Orange

Orange;

Public AppleImpl

Orange

Orange) {

this.range = Orange;

}

// Other Methods

}

Public class appleImpl imports apple {

Private

Orange

Orange;

Public apple () {

THIS.Orange = new OrangeImpl ();

}

// Other Methods

}

Public class appleImpl imports apple {

Private stat

Orange

Orange = OrangeFactory.getraNge ();

Public apple () {

}

// Other Methods

}

The problem here is that you rely on ORANGLEIMPL to implement an ORANGE interface, so that the Apple class lacks sufficient flexibility. These code are hard-written, and they cannot be multiplexed more unable to generate different instances through a ClassLoader.

Module configuration

Sometimes our configuration is as follows:

Public class bigfatcomponent {

String config01;

String config02;

Public BigfatComponent () {

ResourceFactory Resources = New ResourceFactory (New File ("MyComp.Properties");

Config01 = resources.get ("config01");

Config02 = resources.get ("config02");

}

// Other Methods

}

Using IOC mode can be optimized as follows:

Public class bigfatcomponent {

String config01;

String config02;

Public BigfatComponent (String config01, string config02) {

THIS.CONFIG01 = Config01;

THIS.CONFIG02 = Config02;

}

// Other Methods

}

Public interface bigfatcomponfig {

String getconfig01 ();

String getconfig02 ();

}

Public class bigfatcomponent {

String config01; string config02;

Public BigfatComponent (BigfatComponentConfig Config) {

THIS.CONFIG01 = Config.getConfig01 ();

THIS.CONFIG02 = config.getconfig02 ();

}

// Other Methods

}

This way we can flexibly have different implementations for BigfatComponentConfig:

Direct write code to set configuration

Read configuration from XML file

Read configuration from Properties file

Select different implementations as needed

Module lifecycle

The life cycle of the module is usually starting from the constructor. IOC mode recommends that we implements startable interfaces and end modules through the Start / Stop method, which facilitates the container to control the life cycle of the module.

Public class somedaemoncomponent implements startable {

Public void start () {

// listen or whatver

}

Public void stop () {

}

// Other Methods

}

exception

Logs are an exception to IOC, and the Commons-Logging and Log4j under apache do not support IOC mode. The typical usage is to be directly called in the application, and PicoContainer does not recommend that we reuse the log modules.

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

New Post(0)