angular.js - 如何通俗易懂的解释“依赖注入”?
ringa_lee
ringa_lee 2017-05-15 17:04:27
0
7
845

如何通俗易懂的解释“依赖注入”?另外,依赖注入和控制反转有区别吗?具体是什么区别?

ringa_lee
ringa_lee

ringa_lee

reply all (7)
黄舟

Things that have already been written are ready to use!

    世界只因有你

    First of all, what is dependence? An object depends on other objects. For example, var d=new Date(); means that d depends on the creation of Date object.
    Second, is it better to depend on or not? no In order to achieve low coupling of the program, it is better to minimize the interdependence and constraints between components. For example, if a constructor has been created by others, I can call it directly without creating it again.
    Third, what is injection? It means that the object I am using now was given by others and was created passively. For example

    angular.module('M1',['ng','ngRoute']) .controller('C1',function($scope,$http){ $scope.data = 999; $http.get().success(); }) 这段代码运行时:只需要传入所需要的模块的名称,ng底层自动创建相关的对象,直接使用
      阿神

      There is no difference, just different angles, they all refer to the same thing. To put it simply, you don’t need to create objects yourself, spring will help you put the objects where you need them

        滿天的星座

        It’s just a sublimation of the factory model.
        Dependency injection and inversion of control code principles are the same thing, they are just different in understanding.

        These terms are really too advanced. In fact, they are nothing more than some basic object-oriented applications, which have confused many friends.
        This is the same as strategy pattern and factory pattern.

          曾经蜡笔没有小新

          You can refer to: http://www.nowcoder.com/questionTerminal/3be16186465a453f876729acd2e46ddf

            我想大声告诉你

            There is essentially no difference between dependency injection and inversion of control, but the perspective of describing the problem is different.

            Inversion of control:
            What exactly is inverted? The definition from Wikipedia is that the acquisition of dependent objects is inverted.
            Applications are generally composed of many objects. Many other classes need to be used in one class. Initially, we took the initiative to obtain instances of other classes through the new keyword. This brings about a problem: the calling relationship of each class is coupled.With inversion of control, we only need to passively wait for spring to inject the instance of the class into us, and we can use it directly.

            This has many benefits, such as centralized management of objects, no need to implement many singletons by yourself, decoupling the calling relationships of classes, etc.Suppose there are two classes A and B. If a statement like

            is used in A, then add a directed edge from B to A. A larger project may have thousands of classes, and the directed graph formed in this way will definitely be extremely complex. If inversion of control is used, in the most extreme case, all our classes will become independent points.

            Because you have control over each bean, you can also derive various powerful functions.

            spring is built on IoC and AOP.new B()

              迷茫

              There is a database class db, which has a static method get_db() that can obtain the database connection object.
              There is also a class post that needs to operate the database, and it has a method get_post() that needs to query the database.
              Because database connection is a public operation , the post class does not want to connect to the database again internally to avoid coupling. So the post class provides a set_db() method to obtain the database connection object.
              db::get_db() serves as the set_db() of the post class. The parameters of this method are passed into the post class, which is dependency injection.

              db = $db; } public function get_post($id){ return $this->db->query('SELECT * FROM post WHERE id ='.intval($id)); } } $post = new post(); $post->set_db( db::get_db() ); //注入post类依赖的数据库连接对象 var_export( $post->get_post(1024) );
              Compared with the writing method below, you can understand that dependency injection is actually a patch for some languages that are completely OOP.

              query('SELECT * FROM post WHERE id ='.intval($id))->fetch_all(); }
                Latest Downloads
                More>
                Web Effects
                Website Source Code
                Website Materials
                Front End Template
                About us Disclaimer Sitemap
                php.cn:Public welfare online PHP training,Help PHP learners grow quickly!