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

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();
}
}
主站蜘蛛池模板: 宿迁市| 集贤县| 建湖县| 余干县| 武城县| 余江县| 溧阳市| 依兰县| 新源县| 长顺县| 全南县| 那坡县| 福安市| 新平| 焦作市| 吉木乃县| 大竹县| 额济纳旗| 镶黄旗| 宜州市| 南江县| 聂拉木县| 舒兰市| 北川| 义马市| 宣武区| 宜昌市| 麟游县| 洱源县| 锡林浩特市| 海安县| 长春市| 达拉特旗| 东辽县| 贺兰县| 台东市| 尖扎县| 嘉峪关市| 类乌齐县| 蓝山县| 永顺县|