Zen-Dependency inversion principle of design patterns

Source: Internet
Author: User

Zen-Dependency inversion principle of design patterns

 

Dependency inversion principle (DIP): demo (https://github.com/sanyinchen/UMLDemo) 1. the high-level module should not depend on the underlying Module 2. abstraction should not depend on details 3. dependencies between modules occur not through implementation classes, but by abstract classes. the interface or abstract class does not depend on details 5. the implementation class depends on the scenario where the demo provided in the interface or abstract class is a Driver driving a Car. From the uml diagram, we can clearly see that the interfaces ICar and Idriver are coupled, rather than the implementation class. The advantage is that when the driver is driving, drivers can drive different vehicles. Let's take a look at the client scenario:

 

public class Client {public static void main(String[] args) {// TODO Auto-generated method stubIDriver driver=new Driver();ICar car=new BMW();driver.driver(car);car=new Benz();driver.driver(car);}}

 

We can see that there are two different cars in car new, but the Driver can start two different cars without making any changes. Here, the Driver class is a high-level module. When we replace the car of the underlying module, it still does not affect the running dependency of the Driver class: (1) interface declaration (that is, the demo method) (2) constructor injection (3) Advantages of the set injection Dependency inversion principle and operation methods: (1) decoupling between entity classes for parallel development and testing (2) every class has an interface, an abstract class, or an abstract class and an interface as much as possible (this is the basic requirement of the dependency inversion principle. Only an abstract can generate an inverted dependency) (3) the surface type of the variable should be an interface or abstract type (4) no class should be derived from the object class (5) try not to override the abstract class method (the Dependency inversion principle advocates the abstract Part dependency. If the abstract part is overwritten, the stability of the program will be affected) (6) it is used in combination with the lean replacement principle (the interface is responsible for defining public attributes and methods, and declaring dependencies with other classes. The abstract class is responsible for implementing the public part and implementing the class to accurately implement the business logic, at the same time, refine the parent class when appropriate. Finally, let's look at the inversion. Generally, implementation-oriented programming is the Actual dependency of each entity class. This is generally the case, this can be understood as positiveness, so we are not generating dependencies between entity classes, but relying on abstract classes, and then implementing those mutually dependent abstract classes by entity classes.

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.