Design Patterns-Design principles-dependency inversion principle

Source: Internet
Author: User
1: The dependency inversion principle behaves in the Java language:

Dependencies between modules occur through abstraction, and there is no direct dependency between classes, and their dependencies are generated through interfaces or abstract classes.
An interface or abstract class is not dependent on an implementation class.
Implement class dependencies with interfaces or abstract classes.

The dependency inversion principle can reduce the coupling between classes, improve the stability of the system, reduce the risk caused by concurrent exploitation, and improve the readability and maintainability of the code.
Dependencies can be passed on. As long as you do abstract dependencies, even multi-layered dependency transmission is fearless.
Object dependencies are passed in three different ways:
1: Constructors Pass dependent objects
2:setter Method Passing dependent objects
3: interface declares dependent objects

2: Best Practices:

Each class has an interface or abstract class as much as possible, or both.
Interfaces are responsible for defining public properties and methods, and for declaring dependencies on other objects,
Abstract class is responsible for the implementation of the public structure, implementing the class to achieve the exact business logic, while in the appropriate world to the parent class refinement.
The surface type of the variable is as far as the interface or abstract class.
No class should derive from a specific class.
Try not to overwrite (override) The method of the base class.
Combined with the Richter replacement principle.

"Interface-oriented programming" is the core of the dependency inversion principle.

Design Patterns-Design principles-dependency inversion principle

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.