After searching online, I still can’t understand it
淡淡烟草味2017-05-15 16:53:13
Dependency injection: When the program is running, if you need to cooperate with another object (call its method, access its properties), you do not need to create the callee in the code, but rely on the injection of the external container
The following example may be more interesting
Understanding Dependency Injection:
A person (Java instance, caller) needs an ax (Java instance, callee)
In primitive society, there is almost no social division of labor; the person who needs an ax (the caller) can only grind an ax by himself (the callee); the corresponding situation is: the caller in the Java program creates the callee himself, usually Use the new keyword to call the constructor to create a callee
Entering the industrial society, factories appeared. Axes were no longer completed by ordinary people, but were produced in factories. At this time, the person who needed an ax (the caller) found the factory and purchased the ax without caring about the manufacturing process of the ax; the correspondence is simple. Factory design pattern, the caller only needs to locate the factory and does not need to manage the specific implementation of the callee
In a "communist" society, people who need an ax don't even need to locate the factory, and can just "sit back and wait" for the society to provide it; the caller does not need to care about the implementation of the callee, ignore the factory, and wait for Spring dependency injection
In short, dependency injection means that what you need is not created by you, but is provided to you by a third party, or the container. Such a design conforms to orthogonality, the so-called loose coupling.
伊谢尔伦2017-05-15 16:53:13
Personally understood, dependency injection means that the caller can obtain control of a component just by declaring a component, and the dependency management, search, and loading of the component are completed externally.
This work is done by $injector
in AngularJS.
For example, I injected $http
,并用hehe
into a service to call:
.service('myService',['$http',function(hehe) {
this.getUserActivities = function(username){
return hehe({
method: 'JSONP',
url:'https://api.github.com/users/'+username+'/events?callback=JSON_CALLBACK'
});
}
}])
PHP中文网2017-05-15 16:53:13
Dependency injection means that you don’t have to worry about the life cycle of the object, when it is created, and when it is destroyed. You just need to use it directly. The life cycle of the object is managed by the framework that provides dependency injection.
世界只因有你2017-05-15 16:53:13
I would like to add one more thing: Dependency injection and inversion of control are actually the same concept.
@Wang_Bourne
http://www.cnblogs.com/kinglongdai/p/3269049.html
MOOC: http://www.imooc.com/video/4907
怪我咯2017-05-15 16:53:13
Theoretical understanding always feels a bit esoteric. I recommend you to look at some open source frameworks for dependency injection, such as RoboGuice. Real knowledge comes from practice
Sorry, I went to the wrong door. I didn’t read the category. RoboGuice is a framework for android, but the principle is the same
巴扎黑2017-05-15 16:53:13
Dependency Injection (DI) is also called Inversion of Control (IoC), that is, the relationship between classes and methods and methods is "injected" through a third party (such as a configuration file) , there is no need for classes or class methods to resolve the calling relationship between each other. Traditional applications usually actively create other objects that the class depends on in the execution code within the class, resulting in tight coupling between classes, making the class difficult to test and isolate, and ultimately making the expansion and maintenance of the system extremely difficult. Dependency injection is used to solve dependencies, configuration and life cycle management between components. By transferring object control, it can solve the coupling problem between classes. There is a loose coupling relationship between objects. More importantly, it makes the application system The structure has become very flexible, which well embodies one of the object-oriented design rules - the dependency design principle, the famous Hollywood principle: "Don't call us, we'll call you. (Don't call us, we'll call you.) you)
習慣沉默2017-05-15 16:53:13
angular.js contains a lot of this kind of thinking.
include var net = require('net');
They are all purple.
天蓬老师2017-05-15 16:53:13
Dependency Injection
In fact, it means that the characteristic attributes of the current object are determined by the externally injected object.
For example, if your family opens a shoe factory, there is a method of producing shoes called make.
function shoes() {
this.make = function() {
....
}
}
Due to market demand, some big-name sports shoe companies need your factory to produce them. They will give you a large bag of raw materials. Your factory can get the raw materials to produce them and then OEM them.
function shoes(material) {
this.material = material;
this.make = function() {
....
}
}
In this way, if Nike needs you to do OEM work and give you Nike’s material objects, you can produce Nike’s sports shoes. If Adidas gives you their own materials, you can produce their shoes.
In other words, whether the shoes produced by your shoe factory are from company A or company N depends on whether the company that provides you with the materials is company A or company N.
怪我咯2017-05-15 16:53:13
In object-oriented programming, the problem we often deal with is decoupling. The lower the coupling of the program, the higher the readability and maintainability of the program. Inversion of Control (IoC) is a commonly used object-oriented programming design principle. Using this principle we can reduce coupling. Dependency injection is the most commonly used implementation of inversion of control.
Detailed visit to talk about dependency injection
为情所困2017-05-15 16:53:13
Very simple personal understanding, dependency injection is actually not difficult, it is a very natural thing, real life is actually more difficult than programming:
Dependency injection: You are a person, you want to go hunting, you need a gun, There are two options at this time. The first option is to create it yourself, which is obviously not advisable. This is strong coupling. The second option is that you go to the store and buy it, This is dependency injection, Because it is injected into you from the outside, By buying it, you can solve the problems you created, and by injecting it, you can relieve your dependency. What a simple thing.
Inversion of Control Principle: Rely on abstraction, not on concrete things. When you go to buy a gun, you should have the abstract concept of the gun in your mind. You only need to tell the gun store owner that this gun is An abstract image in your mind will do. And you are created by God. When God wants to test you as a hunter, it would be more troublesome for God to create a gun for you, but if there is a gun instruction manual in your bag, it will be easier. God I'll help you get a gun in a minute. To put it more crudely, don’t rely on the specific gun, but rely on the manual of the gun. This is the inversion of control principle. Obviously, this is what we do in real life.
Container: It is equivalent to a super powerful factory in real life. God (programmer) is responsible for constantly upgrading it, usually bind. Whatever you tell him, he can help you produce it and then give it to you for free. for you. The factory does all the dirty work for you, and you are only responsible for enjoying life. This factory is where the difficulty lies in implementing dependency injection.
Dependency injection is equivalent to a communist society.