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
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.
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.
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(); }
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
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.
Compared with the writing method below, you can understand that dependency injection is actually a patch for some languages that are completely OOP.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.