- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 590字
- 2021-07-02 19:38:18
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();
}
}
- 軟件架構設計:大型網站技術架構與業務架構融合之道
- HTML5 and CSS3 Transition,Transformation,and Animation
- Create React App 2 Quick Start Guide
- 微信小程序開發與實戰(微課版)
- Java語言程序設計教程
- Learning YARN
- 工業機器人離線編程
- Magento 2 Beginners Guide
- 深入理解Kafka:核心設計與實踐原理
- Android智能手機APP界面設計實戰教程
- Learning D3.js 5 Mapping(Second Edition)
- Jakarta EE Cookbook
- Python程序設計:基礎與實踐
- 趣味掌控板編程
- PyTorch生成對抗網絡編程