Inversion of Control – Part I

Strategy Design Pattern

Why am I talking about Inversion of Control? I will use Spring (Boot) later on to build services. Spring is based on Dependency Injection. Dependency Injection is one possible implementation of Inversion of Control. Hence, it is reasonable to understand the basics of Inversion of Control (IoC) and how it can be implemented. Before I get to the Sprint implementation, I will explain the concept, the Design Patterns that use IoC and will write about Dependency Injection in Spring in a subsequent blog post.

What is Inversion of Control?

When you write an application (code) you are fully in control of the flow.

public static void main(String args[]) {

    while (true) {
        Scanner scan = new Scanner(System.in);
        String text = scan.nextLine();
        System.out.println(text);
    }
}

However, quite often the control is handed over to a framework that is in control of your code.

Take for example Java Servlets, that I explained in an earlier blog post. In your implementation of the HttpServlet class, you implement a method such as doGet() which handles HTTP GET requests. This method is called whenever a request is made to an URL you specify in your web.xml. Here the servlet container takes control of your class. This allows the elegant implementation of different Servlets that are all controlled the same way.

public class HelloServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public void init() throws ServletException {
    System.out.println("init()");
    super.init();
  }

  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    System.out.println("doGet()");
  }
}

Another example is Event Handling, e.g. in JavaScript. You can bind JavaScript code to an event on an element, e.g. object.addEventListener("click", myScript); Once you click on the element, the JavaScript Engine of your Browser will call myScript.

document.getElementById("submitButton").addEventListener("click", submitForm);

function submitForm() {
   var name = document.getElementById("name");
   // read elements and call service()-method
}

Basically, IoC separates what code is executed from when it is executed. Both parts know as little as possible about each other – there only connection is an interface or abstract class provided by the controlling code. Furthermore, IoC reduces code duplication and encourages the use of Interfaces instead of implementations.

Implementations of Inversion of Control

Some of the well-known Design Patterns by the GoF (Gang of Four –  E. Gamma, R. Helm, R. Johnson and J. Vlissides) implement the Inversion of Control, such as the Observer, Strategy, Factory or the Template design pattern. Other examples are the Service Locator or Dependency Injection. In the next part, we will take a further look at Dependency Injection. In the last part, we will see how Dependency Injection is used with the Spring Framework.

 

 

 

Chris

My background lies in software-development and software-architecture. I have been working for over 20 years for national and multi-national organisations in large and small teams. In 2006 I came in touch with agile values and principles. Since then I am applying these values on a daily basis as an Agile Coach and Scrum Master. However, I still can’t detach myself from software-development. Hence, I develop some minor projects or experiment with technology I hear from the teams I work with.