Inversion of Control (IOC), Dependency Injection (DI) Oversimplified

Introduction

IOC and DI are very useful patterns that are used in almost every production project. But, for me personally, they are pretty misleading and difficult concepts to understand. Here, I'll example what both of them are and implement a simplified proof of concept. 

Inversion of Control

Inversion of control is a pattern responsible for your class initialization. They will be created and set on startup or before each class is initialized. So initialization is not done in constructor but by classes themselves (control is inverted). In a configuration class, you can what implementation each interface will be used. 

[CSF] OfficeFloor: Going Beyond Dependency Injection

So why another Dependency Injection (DI) framework? Because Dependency Injection is only part of the Inversion of (Coupling) Control problem. OfficeFloor provides the complete injection solution.

Dependency Injection Problem

Dependency Injection on it's own can actually promote lower cohesion and higher coupling. Why, because Dependency Injection provides undisciplined short cuts to get references to objects. I need a repository to retrieve some data, I just inject it. I need some logic to work out some result, I just inject it. Overtime with on going improvements to systems, everything starts to reference everything else. Changing the interface of one thing becomes difficult, as so many things just pull it in (higher coupling). Furthermore, because it is so easy to "just inject" dependencies, convenience starts polluting single purpose logic of classes (lower cohesion).

Understanding Inversion of Control and Dependency Injection

There are two key concepts that you must understand when you start with Spring or JakartaEE/CDI–inversion of control and dependency injection.

In brief, inversion of control means letting a framework take control of the execution flow of your program to do things like create instances of your classes and inject the required dependencies.

Spring Framework Basics: What Is Inversion of Control?

Developers starting with the Spring Framework often get confused with the terminology, specifically dependencies, dependency injection, and Inversion of Control. In this article, we introduce you to the concept of Inversion of Control.

What You Will Learn

  • What is Inversion of Control?
  • What are some examples of Inversion of Control?
  • How does the Spring Framework implement Inversion of Control?
  • Why is Inversion of Control important and what are its advantages?

What Is Inversion of Control?

Approach-1

Have a look at the following implementation of ComplexAlgorithmImpl:

Inversion of Control (Explained Non-Technically)

I will use how businesses evolve to provide an analogy for Inversion of Control.

Businesses don't set out on day one to be a Fortune 500 company. Typically, they start with you in your garage (maybe with a friend).Over time, your business grows and you hire people, assign clearer functional responsibilities, and start to scale up your business. Businesses have to do this, while also changing quickly to stay competitive.

Functional Programming Is Just a Safety Restriction on Object Orientation

I find that functional languages are an admiration of it's own idealized self. Their pursuit of mathematical (lambda calculus) purity forgets that systems run in the real world and not in theory. Yes, for academia and advancing compilers, they are great where idealized theory is the endeavour. However, their idealism is neglectful of the limitations and realities of computing for commercial development. Why be shamed for wanting to allow side effects and mutating objects? Especially when functional programming just further restricts object orientation.

Limitations

To illustrate my point of limitations within computers, let's use the typical functional programming example of calculating a particular value for the Fibonacci sequence.