Dependency inversion principle

Source: Internet
Author: User

Definition:High-level modules should not depend on low-level modules, both of which should depend on their abstraction; abstraction should not rely on details; details should rely on abstraction.

Problem:Class A directly depends on Class B. To change Class A to dependent Class C, you must modifyCode. In this scenario, Class A is generally a high-level module responsible for complex business logic. class B and class C are low-level modules responsible for basic atomic operations. If Class A is modifiedProgramBring unnecessary risks.

Solution:Change Class A to dependent interface I. class B and class C implement interface I. Class A indirectly associates with Class B or Class C through interface I, this greatly reduces the chance of modifying Class.

The principle of dependency inversion is based on the fact that abstract things are much more stable than the variability of details. The architecture built on the basis of abstraction is much more stable than the architecture built on the basis of detail. In C #, abstraction refers to interfaces or abstract classes. The details are specific implementation classes. The purpose of using interfaces or abstract classes is to formulate specifications and contracts, instead of involving any specific operations, the detailed tasks are handed over to their implementation classes.

The core idea of the principle of dependency inversion is:Interface-oriented programming,We still use an example to illustrate what better interface-oriented programming is than implementation-oriented programming. This is the case. When a mother tells a story to her child, she can write a story to the child according to the book. The Code is as follows:

   Class Book
{
Public String getcontent ()
{
Return " A long time ago there was an Arabic story ...... " ;
}
}

Class Mother
{
Public Void Narrate (book)
{
Console. writeline ( " Mom started telling stories " );
Console. writeline (book. getcontent ());
}
}

Public Class Client
{
Public Static Void Main (string [] ARGs)
{
Mother = New Mother ();
Mother. narrate ( New Book ());
}
}

Running result:

Mom started telling stories
A long time ago there was an Arabic story ......

The newspaper runs well. If one day, the demand becomes like this: instead of giving a book but a newspaper, let the mother tell the story of the newspaper. The newspaper code is as follows:

 
ClassNewspaper {
PublicString getcontent (){
Return "Jeremy Lin leads the Knicks in 38 + 7 victory over the Lakers ......";
}
}

This mother could not do it because she could not read the story in the newspaper. It was so absurd that she had to change the book into a newspaper that she had to modify mother to read it. What if I want to change to a magazine in the future? What about changing to a webpage? We need to constantly modify mother, which is obviously not a good design. The reason is that the coupling between mother and book is too high, and the coupling between them must be reduced.

We introduce an abstract interface ireader. Reading books, as long as they contain words, all belong to reading books:

InterfaceIreader {
PublicString getcontent ();
}

The mother class is dependent on the ireader interface, while both book and newspaper belong to the category of reading books. They implement the ireader interface respectively, so that they conform to the dependency inversion principle and the code is changed:

  Interface Ireader
{
Public String getcontent ();
}
Class Newspaper: ireader
{
Public String getcontent ()
{
Return " Jeremy Lin helped the Knicks beat the hawks in 17 + 9 ...... " ;
}
}
Class Book: ireader
{
Public String getcontent ()
{
Return " A long time ago there was an Arabic story ...... " ;
}
}

Class Mother
{
Public Void Narrate (ireader reader)
{
Console. writeline ( " Mom started telling stories " );
Console. writeline (reader. getcontent ());
}
}

Public Class Client
{
Public Static Void Main (string [] ARGs)
{
Mother = New Mother ();
Mother. narrate ( New Book ());
Mother. narrate ( New Newspaper ());
}
}

Running result:

Mom started telling stories
A long time ago there was an Arabic story ......
Mom started telling stories
Jeremy Lin helped the Knicks beat the hawks in 17 + 9 ......

After this modification, no matter how the client class is extended in the future, you do not need to modify the mother class. This is just a simple example. In actual situations, the mother class representing the high-level module will be responsible for completing the main business logic. Once it needs to be modified, it is highly risky to introduce errors. Therefore, following the Dependency inversion principle can reduce coupling between classes, improve system stability, and reduce the risks caused by program modification.

Using the Dependency inversion principle brings great convenience to concurrent development by many people. For example, in the previous example, when the mother class is directly coupled with the book class, the mother class can be encoded only after the book class is encoded, because the mother class depends on the book class. The modified program can be started at the same time without affecting each other, because Mother has nothing to do with the book class. The more people participate in collaborative development and the larger the project, the more significant the adoption of dependency-resulting principles. The popular TDD development model is the most successful application based on the inverted principle.

There are three ways to pass dependencies. the method used in the preceding example isInterface transfer,There are two other transmission methods:ConstructorAndSetter method transfer,I believe that if you have used the Spring framework, you will not be unfamiliar with the dependency transmission method. C ..
In actual programming, we generally need to do the following three points:

    • The lower-level modules should have abstract classes or interfaces, or both.
    • The Declaration type of variables should be abstract classes or interfaces.
    • Use inheritance follows the principle of RYS replacement.

The core of the dependency inversion principle is to us.Interface-Oriented Programming, Understand interface-oriented programming, and understand Dependency inversion.

From: http://blog.csdn.net/zhengzhb/article/details/7289269

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.