nu Main menu move to sidebar hide Navigation Main page Contents Current events Random article About Wikipedia Contact us Contribute Help Learn to edit Community portal Recent changes Upload file Special pages Search Search Appearance Donate Create account Log in Personal tools Donate Create account Log in Contents move to sidebar hide (Top) 1 Alternative meaning 2 Overview 3 Background 4 Description 5 Use 6 Example code Toggle Example code subsection 6.1 HTML DOM events 6.2 Web application frameworks 7 See also 8 References 9 External links Toggle the table of contents Inversion of control 17 languages العربية Deutsch Español فارسی Français עברית Magyar Italiano 日本語 Qaraqalpaqsha 한국어 Polski Português Русский Svenska Українська 中文 Edit links Article Talk English Read Edit View history Tools Tools move to sidebar hide Actions Read Edit View history General What links here Related changes Upload file Permanent link Page information Cite this page Get shortened URL Print/export Download as PDF Printable version In other projects Wikidata item Appearance move to sidebar hide From Wikipedia, the free encyclopedia Software programming technique In software design , inversion of control ( IoC ) is a design principle in which custom-written portions of a computer program receive the flow of control from an external source (e.g. a framework ). The term "inversion" is historical: a software architecture with this design "inverts" control as compared to procedural programming . In procedural programming, a program's custom code calls reusable libraries to take care of generic tasks, but with inversion of control, it is the external code or framework that is in control and calls the custom code. Inversion of control has been widely used by application development frameworks since the rise of GUI environments [ 1 ] [ 2 ] and continues to be used both in GUI environments and in web server application frameworks . Inversion of control makes the framework extensible by the methods defined by the application programmer. [ 3 ] Event-driven programming is often implemented using IoC so that the custom code need only be concerned with the handling of events, while the event loop and dispatch of events/messages is handled by the framework or the runtime environment. In web server application frameworks, dispatch is usually called routing, and handlers may be called endpoints. Alternative meaning [ edit ] The phrase "inversion of control" has separately also come to be used in the community of Java programmers to refer specifically to the patterns of dependency injection (passing services to objects that need them) that occur with "IoC containers" in Java frameworks such as the Spring Framework . [ 4 ] In this different sense, "inversion of control" refers to granting the framework control over the implementations of dependencies that are used by application objects [ 5 ] rather than to the original meaning of granting the framework control flow (control over the time of execution of application code, e.g. callbacks). Overview [ edit ] As an example, with traditional programming, the main function of an application might make function calls into a menu library to display a list of available commands and query the user to select one. [ 6 ] The library thus would return the chosen option as the value of the function call, and the main function uses this value to execute the associated command. This style was common in text-based interfaces . For example, an email client may show a screen with commands to load new mail, answer the current mail, create new mail, etc., and the program execution would block until the user presses a key to select a command. With inversion of control, on the other hand, the program would be written using a software framework that knows common behavioral and graphical elements, such as windowing systems , menus, controlling the mouse, and toolbars. The custom code "fills in the blanks" for the framework, such as supplying a table of menu items and registering a code subroutine for each item, but it is the framework that monitors the user's actions and invokes the subroutine when a menu item is selected. In the mail client example, the framework could follow both the keyboard and mouse inputs and call the command invoked by the user by either means and at the same time monitor the network interface to find out if new messages arrive and refresh the screen when some network activity is detected. The same framework could be used as the skeleton for a spreadsheet program or a text editor. Conversely, the framework knows nothing about Web browsers, spreadsheets, or text editors; implementing their functionality takes custom code. Inversion of control carries the strong connotation that the reusable code and the problem-specific code are developed independently even though they operate together in an application. Callbacks , schedulers , event loops , and the template method are examples of design patterns that follow the inversion of control principle, although the term is most commonly used in the context of object-oriented programming . ( Dependency injection is an example of the separate, specific idea of "inverting control over the implementations of dependencies" popularised by Java frameworks.) [ 4 ] Inversion of control is sometimes referred to as the "Hollywood Principle: Don't call us, we'll call you," reflecting how frameworks dictate execution flow. [ 1 ] Background [ edit ] The etymology of the phrase has been traced back to 1988, [ 7 ] but it is closely related to the concept of program inversion described by Michael Jackson in his Jackson Structured Programming methodology in the 1970s. [ 8 ] A bottom-up parser can be seen as an inversion of a top-down parser : in the one case, the control lies with the parser, while in the other case, it lies with the receiving application. The term was used by Michael Mattsson in a thesis (with its original meaning of a framework calling application code instead of vice versa) [ 9 ] and was then taken from there [ 10 ] by Stefano Mazzocchi and popularized by him in 1999 in a defunct Apache Software Foundation project, Avalon, in which it referred to a parent object passing in a child object's dependencies in addition to controlling execution flow. [ 11 ] The phrase was further popularized in 2004 by Robert C. Martin and Martin Fowler , the latter of whom traces the term's origins to the 1980s. [ 7 ] Description [ edit ] In traditional programming, the flow of the business logic is determined by objects that are statically bound to one another. With inversion of control, the flow depends on the object graph that is built up during program execution. Such a dynamic flow is made possible by object interactions that are defined through abstractions. This run-time binding is achieved by mechanisms such as dependency injection or a service locator . In IoC, the code could also be linked statically during compilation, but finding the code to execute by reading its description from external configuration instead of with a direct reference in the code itself. In dependency injection, a dependent object or module is coupled to the object it needs at run time . Which particular object will satisfy the dependency during program execution typically cannot be known at compile time using static analysis . While described in terms of object interaction here, the principle can apply to other programming methodologies besides object-oriented programming . In order for the running program to bind objects to one another, the objects must possess compatible interfaces . For example, class A may delegate behavior to interface I which is implemented by class B ; the program instantiates A and B , and then injects B into A . Use [ edit ] The Mesa Programming environment for XDE , 1985 [ 1 ] Visual Basic (classic) , 1991. HTML DOM event The Spring Framework [ 5 ] ASP.NET Core [ 12 ] Template method pattern Example code [ edit ] HTML DOM events [ edit ] Web browsers implement inversion of control for DOM events in HTML. The application developer uses document.addEventListener() to register a callback. <!doctype html> < html lang = "en" > < head > < meta charset = "utf-8" > < title > DOM Level 2 </ title > </ head > < body > < h1 > DOM Level 2 Event handler </ h1 > < div id = "output" ></ div > < script > var listener = function () { document . getElementById ( "output" ). innerHTML = "<p>The registered listener was called.</p>" ; } document . addEventListener ( "click" , listener , true ); document . getElementById ( "output" ). innerHTML = "<p>The event handler has been registered. If you click the page, your web browser will call the event handler.</p>" </ script > </ body > </ html > Web application frameworks [ edit ] This example code for an ASP.NET Core web application creates a web application host, registers an endpoint, and then passes control to the framework: [ 12 ] var builder = WebApplication . CreateBuilder ( args ); var app = builder . Build (); app . MapGet ( "/" , () => "Hello World!" ); app . Run (); See also [ edit ] Abstraction layer Asynchronous I/O Aspect-oriented programming Callback (computer programming) Closure (computer programming) Continuation Delegate (CLI) Dependency inversion principle Flow-based programming Implicit invocation Interrupt handler Message passing Monad (functional programming) Observer pattern Publish–subscribe pattern Service locator pattern Signal (computing) Software framework Strategy pattern User exit Visitor pattern XSLT References [ edit ] ^ a b c Sweet, Richard (25 June 1985). "The Mesa Programming Environment" . ACM SIGPLAN Notices . 20 (7): 216– 229. doi : 10.1145/17919.806843 . ^ Visual_Basic_(classic) ^ Johnson, Ralph E.; Foote, Brian (June–July 1988). "Designing Reusable Classes" . Journal of Object-Oriented Programming . 1 (2): 22– 35. CiteSeerX   10.1.1.101.8594 . Retrieved 29 April 2014 . ^ a b Fowler, Martin. "Inversion of Control Containers and the Dependency Injection pattern" . MartinFowler.com . Retrieved 4 June 2023 . ^ a b "Spring Framework The IoC container" . docs.spring.io . Retrieved 25 May 2023 . ^ Dependency Injection . ^ a b Inversion of Control on Martin Fowler's Bliki ^ "Introduction to Jackson Design Method" (PDF) . ^ Mattsson, Michael (February 1996). "Object-Oriented Frameworks, A survey of methodological issues" . Department of Computer Science, Lund University. CiteSeerX   10.1.1.36.1424 . LU-CS-TR: 96-167. ^ Stefano Mazzocchi (22 January 2004). "On Inversion of Control" . Archived from the original on 2 February 2004. ^ "IOC Patterns - Avalon Framework" . The Apache Avalon Project . Retrieved 8 June 2023 . ^ a b Ryan Nowak, Kirk Larkin, Rick Anderson. " "Routing in ASP.Net Core" " . learn.microsoft.com . microsoft . Retrieved 25 May 2023 . Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints. Endpoints are the app's units of executable request-handling code. Endpoints are defined in the app and configured when the app starts. {{ cite web }} : CS1 maint: multiple names: authors list ( link ) External links [ edit ] Inversion of Control explanation and implementation example Inversion of Control v t e Software design patterns Gang of Four patterns Creational Abstract factory Builder Factory method Prototype Singleton Structural Adapter Bridge Composite Decorator Facade Flyweight Proxy Behavioral Chain of responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template method Visitor Concurrency patterns Active object Balking Binding properties Double-checked locking Event-based asynchronous Guarded suspension Join Lock Monitor Proactor Reactor Read–write lock Scheduler Scheduled-task pattern Semaphore Thread pool Thread-local storage Architectural patterns Front controller Interceptor MVC MVP MVVM ADR ECS n -tier Specification Publish–subscribe Naked objects Service locator Active record Identity map Data access object (DAO) Data transfer object (DTO) Inversion of control Model 2 Broker Other patterns Blackboard Business delegate Composite entity Composition over inheritance Dependency injection Guard clause Intercepting filter Lazy loading Mock object Null object Object pool Servant Twin Type tunnel Method chaining Delegation Books Design Patterns Enterprise Integration Patterns People Christopher Alexander Erich Gamma Ralph Johnson John Vlissides Grady Booch Kent Beck Ward Cunningham Martin Fowler Robert Martin Jim Coplien Douglas Schmidt Linda Rising Communities The Hillside Group Portland Pattern Repository See also Anti-pattern Architectural pattern Retrieved from " https://en.wikipedia.org/w/index.php?title=Inversion_of_control&oldid=1335279198 " Categories : Software architecture Architectural pattern (computer science) Programming principles Component-based software engineering Software design patterns Hidden categories: CS1 maint: multiple names: authors list Articles with short description Short description is different from Wikidata Use dmy dates from March 2021 This page was last edited on 28 January 2026, at 09:59  (UTC) . Text is available under the Creative Commons Attribution-ShareAlike 4.0 License ; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy . Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. , a non-profit organization. Privacy policy About Wikipedia Disclaimers Contact Wikipedia Legal & safety contacts Code of Conduct Developers Statistics Cookie statement Mobile view Search Search Toggle the table of contents Inversion of control 17 languages Add topic