What is Inversion of Control?

前端 未结 30 2671
清歌不尽
清歌不尽 2020-11-22 00:13

Inversion of Control (IoC) can be quite confusing when it is first encountered.

  1. What is it?
  2. Which problem does it solve?
  3. When is it appropria
相关标签:
30条回答
  • 2020-11-22 00:19

    Suppose you are an object. And you go to a restaurant:

    Without IoC: you ask for "apple", and you are always served apple when you ask more.

    With IoC: You can ask for "fruit". You can get different fruits each time you get served. for example, apple, orange, or water melon.

    So, obviously, IoC is preferred when you like the varieties.

    0 讨论(0)
  • 2020-11-22 00:19

    Using IoC you are not new'ing up your objects. Your IoC container will do that and manage the lifetime of them.

    It solves the problem of having to manually change every instantiation of one type of object to another.

    It is appropriate when you have functionality that may change in the future or that may be different depending on the environment or configuration used in.

    0 讨论(0)
  • 2020-11-22 00:20

    A very simple written explanation can be found here

    http://binstock.blogspot.in/2008/01/excellent-explanation-of-dependency.html

    It says -

    "Any nontrivial application is made up of two or more classes that collaborate with each other to perform some business logic. Traditionally, each object is responsible for obtaining its own references to the objects it collaborates with (its dependencies). When applying DI, the objects are given their dependencies at creation time by some external entity that coordinates each object in the system. In other words, dependencies are injected into objects."

    0 讨论(0)
  • 2020-11-22 00:24

    Inversion of Control is what you get when your program callbacks, e.g. like a gui program.

    For example, in an old school menu, you might have:

    print "enter your name"
    read name
    print "enter your address"
    read address
    etc...
    store in database
    

    thereby controlling the flow of user interaction.

    In a GUI program or somesuch, instead we say:

    when the user types in field a, store it in NAME
    when the user types in field b, store it in ADDRESS
    when the user clicks the save button, call StoreInDatabase
    

    So now control is inverted... instead of the computer accepting user input in a fixed order, the user controls the order in which the data is entered, and when the data is saved in the database.

    Basically, anything with an event loop, callbacks, or execute triggers falls into this category.

    0 讨论(0)
  • 2020-11-22 00:24

    Inversion of control is when you go to the grocery store and your wife gives you the list of products to buy.

    In programming terms, she passed a callback function getProductList() to the function you are executing - doShopping().

    It allows user of the function to define some parts of it, making it more flexible.

    0 讨论(0)
  • 2020-11-22 00:24

    I understand that the answer has already been given here. But I still think, some basics about the inversion of control have to be discussed here in length for future readers.

    Inversion of Control (IoC) has been built on a very simple principle called Hollywood Principle. And it says that,

    Don't call us, we'll call you

    What it means is that don't go to the Hollywood to fulfill your dream rather if you are worthy then Hollywood will find you and make your dream comes true. Pretty much inverted, huh?

    Now when we discuss about the principle of IoC, we use to forget about the Hollywood. For IoC, there has to be three element, a Hollywood, you and a task like to fulfill your dream.

    In our programming world, Hollywood represent a generic framework (may be written by you or someone else), you represent the user code you wrote and the task represent the thing you want to accomplish with your code. Now you don't ever go to trigger your task by yourself, not in IoC! Rather you have designed everything in such that your framework will trigger your task for you. Thus you have built a reusable framework which can make someone a hero or another one a villain. But that framework is always in charge, it knows when to pick someone and that someone only knows what it wants to be.

    A real life example would be given here. Suppose, you want to develop a web application. So, you create a framework which will handle all the common things a web application should handle like handling http request, creating application menu, serving pages, managing cookies, triggering events etc.

    And then you leave some hooks in your framework where you can put further codes to generate custom menu, pages, cookies or logging some user events etc. On every browser request, your framework will run and executes your custom codes if hooked then serve it back to the browser.

    So, the idea is pretty much simple. Rather than creating a user application which will control everything, first you create a reusable framework which will control everything then write your custom codes and hook it to the framework to execute those in time.

    Laravel and EJB are examples of such a frameworks.

    Reference:

    https://martinfowler.com/bliki/InversionOfControl.html

    https://en.wikipedia.org/wiki/Inversion_of_control

    0 讨论(0)
提交回复
热议问题