What is Inversion of Control?

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

    IoC is about inverting the relationship between your code and third-party code (library/framework):

    • In normal s/w development, you write the main() method and call "library" methods. You are in control :)
    • In IoC the "framework" controls main() and calls your methods. The Framework is in control :(

    DI (Dependency Injection) is about how the control flows in the application. Traditional desktop application had control flow from your application(main() method) to other library method calls, but with DI control flow is inverted that's framework takes care of starting your app, initializing it and invoking your methods whenever required.

    In the end you always win :)

提交回复
热议问题