Software Development; Coding Techniques; Computer Science
Inversion of control is a software development methodology that reverses the control flow used by traditional procedural programming. In inversion of control, the program responds to calls from a framework rather than controlling program flow itself. This is often used in object-oriented programming.
Inversion of control is an approach to software design that is used to create a software architecture with looser coupling and fewer dependencies. Inversion of control reverses the typical control flow (the order in which a program executes commands) found in applications developed using more traditional methods. In procedural programming, for example, a program will call procedures during execution. Upon being called, those procedures, also called “functions” or “subroutines,” will respond by performing specific predefined tasks, then returning one or more values to the larger program. The overall program is designed to be project specific, while the procedures are typically called from a library of reusable code.
When using inversion of control, this process is reversed: rather than calling procedures from a library, the program responds to calls issued by a software framework that provides generic functionality. In this scenario, the entity issuing the calls (the framework), not the library of procedures that respond to the calls, can be used by other applications. Because the program waits for the framework to initiate calls, inversion of control is sometimes called the “Hollywood principle” in reference to the old movie-industry saying “Don't call us, we'll call you.”
A key concept underlying inversion of control is that software components are bound to one another at run time. This differs from traditional programming, where components are statically bound together when the program is compiled. The run-time binding found in inversion of control is possible because interactions between components are defined through abstractions.
Another core principle of inversion of control is that the execution of a task should be decoupled from its implementation. In other words, an application can invoke and use the functionality of another software component without knowing how that component accomplishes its functionality. In addition, individual components should contain only the code that provides the specific functionality they were designed to implement, allowing other components to provide other functionality as needed. Inversion of control is ideally suited for use in object-oriented programming, where looser coupling and reduced dependencies make it easier to alter or reuse objects and classes.
However, inversion of control also presents some drawbacks. Frameworks and other components can be large, complex, and difficult to understand. New frameworks and components are constantly being created, and changes and updates to existing frameworks and components happen periodically. Thus, a developer who implements inversion of control may have to spend extra time keeping up with the changes. Many frameworks are proprietary, so the internal code used in the framework cannot be studied or modified. Lastly, because inversion of control differs greatly from traditional development models, developers who have used traditional approaches for a long time may be reluctant to embrace this paradigm.
Inversion of control is commonly used by programs that are designed to run on operating systems such as Microsoft Windows. Such programs must respond to events initiated by the user, such as clicking a mouse button or pressing a key on the keyboard. For example, a program might need to display a message box when the user presses the mouse button. Using the Visual Basic programming language, this can be accomplished as follows.
First, a procedure is created that detects when the user clicks the mouse and then sets the variable mouseClicked to “true” to indicate to the program that the mouse click has occurred:
Next, a subroutine is created to respond to the mouse click. This routine uses a loop that executes until the mouse is clicked and the variable mouse-Clicked is set to “true” by the above function. Inside loop, Application.DoEvents() is called, which keeps the program from freezing while the code waits for the mouse to be clicked. When the mouse button is clicked and mouseClicked is set to “true,” the loop exits and the MsgBox function is used to display the message to the user:
This is a classic example of inversion of control, as the specific functionality (display a message to the user) is invoked when the mouse is clicked and the framework notifies the program that a mouse click has occurred. In this scenario, the program responds to events generated by the framework (mouse clicks) rather than controlling the flow of actions itself.
Inversion of control brings the power of frameworks and other components to programming, allowing programs to greatly expand the functionality they provide. Due to the widespread use of inversion of control, numerous frameworks and other components are available for use. As such, programs being developed for a wide variety of business sectors can benefit from the use of inversion of control. As new frameworks and components are developed regularly, inversion of control should remain a relevant methodology for implementing software systems for some time to come.
—Maura Valentino, MSLIS
Bell, Michael. Incremental Software Architecture: A Method for Saving Failing IT Implementations. John Wiley & Sons, 2016.
Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.
Jayaswal, Bijay K., and Peter C. Patton. Design for Trustworthy Software: Tools, Techniques, and Methodology of Developing Robust Software. Prentice Hall, 2007.
MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.
Scott, Michael L. Programming Language Pragmatics. 4th ed., Elsevier, 2016.
Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.
Wysocki, Robert K. Effective Project Management: Traditional, Agile, Extreme. 7th ed., John Wiley & Sons, 2014.