Understanding of dependency Injection, inversion of control, reflection

Source: Internet
Author: User
Tags reflection
Dependency Reversal Principles: In design mode, in order to follow the opening and shutting principle, reduce the dependency between class and class, and design Dependency ReversalPrinciple: high-level should not rely on the lower, both should rely on abstraction; The abstraction does not depend on the figurative, and the elephant relies on abstraction. Understanding: In a project, the high-level control logic directly relies on the lower database (SQL), directly in the logical class to create the database object, forming a close dependency on the database. So if the user needs to change the database (access), and the high-level logic is completely unchanged, then replace the database, not only to add extension code, but also to modify the source. But break the principle of open and close, not easy to vecuronium not easy to re-use flexible bar bar. Therefore, the database should be designed in a high-level abstraction, that is, abstract classes or interfaces, high-level relies on abstraction, as needed to create the desired object from the interface, lower-layer SQL and access need to implement the interface. When you add access, you only need to expand and not modify the original code.
Reflection: Instead of creating the object with new, it uses the ClassLoader to create the class based on the string name, and the right to create the class is transferred, not the caller new but to the IOC control container to create the inversion, which is the key technique for controlling inversion. In Java, use the Assembly.Load (collection name). CreateInstance (collection name, class name) to implement reflection, which is created by the IOC container according to the class loader. Objects are not created in advance, but are created dynamically when used.
Control Inversion:Using the reflection technique, the object creation right is transferred to the container, which is usually combined with the configuration file, and the read creates the object according to the configuration file, eliminating the switch and if statement which is used to determine which object to create.
Dependency Injection: Dependency injection is also a technology to implement control inversion, in fact, we often use in the project, that is, the instantiation of the object is injected by another class, either the container is created and then injected, or it can be created in another class after injection, not to create, but also to turn control to self-sufficiency of others. You can use a constructor or a set function, such as:
public class logic{
MyDataBase DB;
Public Logic (MyDataBase mdb) {
db = MDB;
}
Or
public void Setmydatabase (MyDataBase mdb) {
db = MDB;
}
......
}
Parameters are instantiated objects, not their own new, called Dependency injection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.