What is Inversion of Control?

前端 未结 30 2843
清歌不尽
清歌不尽 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:30

    IoC / DI to me is pushing out dependencies to the calling objects. Super simple.

    The non-techy answer is being able to swap out an engine in a car right before you turn it on. If everything hooks up right (the interface), you are good.

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

    It seems that the most confusing thing about "IoC" the acronym and the name for which it stands is that it's too glamorous of a name - almost a noise name.

    Do we really need a name by which to describe the difference between procedural and event driven programming? OK, if we need to, but do we need to pick a brand new "bigger than life" name that confuses more than it solves?

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

    I agree with NilObject, but I'd like to add to this:

    if you find yourself copying an entire method and only changing a small piece of the code, you can consider tackling it with inversion of control

    If you find yourself copying and pasting code around, you're almost always doing something wrong. Codified as the design principle Once and Only Once.

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

    I shall write down my simple understanding of this two terms:

    For quick understanding just read examples*
    

    Dependency Injection(DI):
    Dependency injection generally means passing an object on which method depends, as a parameter to a method, rather than having the method create the dependent object.
    What it means in practice is that the method does not depends directly on a particular implementation; any implementation that meets the requirements can be passed as a parameter.

    With this objects tell thier dependencies. And spring makes it available.
    This leads to loosely coupled application development.

    Quick Example:EMPLOYEE OBJECT WHEN CREATED,
                  IT WILL AUTOMATICALLY CREATE ADDRESS OBJECT
       (if address is defines as dependency by Employee object)
    

    Inversion of Control(IoC) Container:
    This is common characteristic of frameworks, IOC manages java objects
    – from instantiation to destruction through its BeanFactory.
    -Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean's scope, lifecycle events, and any AOP features for which it has been configured and coded.

    QUICK EXAMPLE:Inversion of Control is about getting freedom, more flexibility, and less dependency. When you are using a desktop computer, you are slaved (or say, controlled). You have to sit before a screen and look at it. Using keyboard to type and using mouse to navigate. And a bad written software can slave you even more. If you replaced your desktop with a laptop, then you somewhat inverted control. You can easily take it and move around. So now you can control where you are with your computer, instead of computer controlling it.

    By implementing Inversion of Control, a software/object consumer get more controls/options over the software/objects, instead of being controlled or having less options.

    Inversion of control as a design guideline serves the following purposes:

    There is a decoupling of the execution of a certain task from implementation.
    Every module can focus on what it is designed for.
    Modules make no assumptions about what other systems do but rely on their contracts.
    Replacing modules has no side effect on other modules
    I will keep things abstract here, You can visit following links for detail understanding of the topic.
    A good read with example

    Detailed explanation

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

    What is Inversion of Control?

    If you follow these simple two steps, you have done inversion of control:

    1. Separate what-to-do part from when-to-do part.
    2. Ensure that when part knows as little as possible about what part; and vice versa.

    There are several techniques possible for each of these steps based on the technology/language you are using for your implementation.

    --

    The inversion part of the Inversion of Control (IoC) is the confusing thing; because inversion is the relative term. The best way to understand IoC is to forget about that word!

    --

    Examples

    • Event Handling. Event Handlers (what-to-do part) -- Raising Events (when-to-do part)
    • Dependency Injection. Code that constructs a dependency (what-to-do part) -- instantiating and injecting that dependency for the clients when needed, which is usually taken care of by the DI tools such as Dagger (when-to-do-part).
    • Interfaces. Component client (when-to-do part) -- Component Interface implementation (what-to-do part)
    • xUnit fixture. Setup and TearDown (what-to-do part) -- xUnit frameworks calls to Setup at the beginning and TearDown at the end (when-to-do part)
    • Template method design pattern. template method when-to-do part -- primitive subclass implementation what-to-do part
    • DLL container methods in COM. DllMain, DllCanUnload, etc (what-to-do part) -- COM/OS (when-to-do part)
    0 讨论(0)
  • 2020-11-22 00:37

    Inversion of Controls is about separating concerns.

    Without IoC: You have a laptop computer and you accidentally break the screen. And darn, you find the same model laptop screen is nowhere in the market. So you're stuck.

    With IoC: You have a desktop computer and you accidentally break the screen. You find you can just grab almost any desktop monitor from the market, and it works well with your desktop.

    Your desktop successfully implements IoC in this case. It accepts a variety type of monitors, while the laptop does not, it needs a specific screen to get fixed.

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