官术网_书友最值得收藏!

What is IOC?

Many a times, the Hollywood principle of Don't call us, we will call you is used to explain the concept of Inversion of Control. In Hollywood, this is what one gets to hear after auditioning is done. If we apply this principle to the programming model, instead of the classes creating dependencies along with serving functionality, the dependencies are appropriately made available to the classes during runtime, and the classes just need to focus on delivering the functionality.

Simply speaking, Inversion of Control (IOC) is about inverting the flow of control that the traditional programming model used to have in terms of objects at the higher-level handling the creation and management of lower-level objects' (can also be termed as dependencies) life cycle. In the IOC programming model, higher-level objects rather receive one or more instances of these dependencies from the calling object or external framework. This is why IOC is also termed Dependency Injection, wherein the dependencies are injected appropriately, and, objects bother themselves solely with the program execution and not with the object creation. Inversion of Control is related to the object-oriented principle known as the Dependency Inversion Principle (DIP), coined by Robert Martin (Uncle Bob).

Let's take a look at the following code sample, which represents higher-level objects handling the creation of lower-level objects (dependencies):

    /*
* This class demonstrates the dependency of higher-level object
* (DrawWithoutIOC)
* onto lower level objects such as Rectangle, Square which are
* created within
* Shape class based on the value of shapeType which is passed as a
* method
* parameter to draw method.
*/

public class DrawWithoutIOC {

Logger logger = Logger.getLogger(DrawWithoutIOC.class.getName());

public void draw(String shapeType) {
Shape shape = new Shape();
try {
shape.draw(shapeType);
}
catch (UndefinedShapeException e) {
logger.log(Level.INFO, e.getMessage(), e);
}
}
/*
* Note that Shape class creates instances of Rectangle or Square
class
* based on the value of shapeType. Any new value that needs to be
supported requires change in the draw method of Shape class.
*/

private class Shape {
public void draw(String shapeType) throws
UndefinedShapeException
{
if(shapeType.equals("rectangle")) {
Rectangle rectangle = new Rectangle();
rectangle.draw();
} else if(shapeType.equals("square")) {
Square square = new Square();
square.draw();
} else {
String shapeNotSupportedMessage = "Shape " + shapeType + "
not supported";
logger.log(Level.INFO, shapeNotSupportedMessage);
throw new UndefinedShapeException
(shapeNotSupportedMessage);
}
}
}

public static void main(String[] args) {
DrawWithoutIOC drawWithoutIOC = new DrawWithoutIOC();
drawWithoutIOC.draw("circle");
}
}

Let us take a look at the class DrawWithIOC, which accepts the implementation of the Shape object in its public constructor. Note that the dependencies, such as different implementations of the Shape object, are rather injected, and code just does the execution of business logic without bothering about creation of objects related to the different implementations of Shape. Other alternatives to injecting the dependencies are passing arguments to a factory method of the class, or through properties that are set on the object instance:

    /**
* In this class, the Shape is passed as parameter to DrawWithIOC
* constructor.
* draw method on a DrawWithIOC object just invokes the draw method
* on Shape object.
* It, no longer, manage the creation of Shape object based on
* shapeType and there upon, invoke the draw method.
**/

public class DrawWithIOC {
Logger logger = Logger.getLogger(DrawWithIOC.class.getName());

private Shape shape;

public DrawWithIOC(Shape shape) {
this.shape = shape;
}

public void draw() {
this.shape.draw();
}

public static void main(String[] args) {
Shape shape = new Rectangle();
DrawWithIOC drawWithIOC = new DrawWithIOC(shape);
drawWithIOC.draw();
}
}
主站蜘蛛池模板: 五莲县| 孟津县| 乌鲁木齐县| 五大连池市| 体育| 潮州市| 大荔县| 内江市| 吴忠市| 黄骅市| 四平市| 孟津县| 南平市| 石台县| 乐山市| 渭源县| 漳浦县| 胶南市| 石景山区| 华容县| 永吉县| 久治县| 太和县| 绩溪县| 卓资县| 宾川县| 清流县| 富平县| 商河县| 高碑店市| 逊克县| 开江县| 龙岩市| 吴忠市| 开化县| 凯里市| 禄丰县| 武隆县| 芜湖市| 威信县| 达拉特旗|