Home  >  Q&A  >  body text

angular.js - 如何通俗易懂的解释“依赖注入”?

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

ringa_leeringa_lee2737 days ago934

reply all(7)I'll reply

  • 黄舟

    黄舟2017-05-15 17:06:27

    Things that have already been written are ready to use!

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-15 17:06:27

    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底层自动创建相关的对象,直接使用

    reply
    0
  • 阿神

    阿神2017-05-15 17:06:27

    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

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-15 17:06:27

    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.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-15 17:06:27

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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-15 17:06:27

    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()

    reply
    0
  • 迷茫

    迷茫2017-05-15 17:06:27

    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.

    <?php
    class db {
        public static function get_db() {
            return new mysqli('127.0.0.1','user','pass','dbname',3306);
        }
    }
    class post {
        private $db;
        public function set_db($db){
            $this->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.

    <?php
    function db() {
        static $db;
        if ($db) {
            return $db;
        } else {
            $db = new mysqli('127.0.0.1','user','pass','dbname',3306);
            return $db;
        }
    }
    function get_post($id) {
        $db = db();
        return $db->query('SELECT * FROM post WHERE id ='.intval($id))->fetch_all();
    }

    reply
    0
  • Cancelreply