[Zen Reading Notes of design patterns] 003 _ six principles of design patterns (III): dependence inversion principle)

Source: Internet
Author: User

Preface

The concept of dependency inversion seems very mysterious, but it is actually very simple. This is also the mentality of reading all technical books. If I overcome this book with my mind, then its content will be easy to understand. The English definition of dependency inversion is as follows:

High Level modules shocould not depend upon Low Level modules. Both shoshould depend upon quota actions. Quota actions shocould not depend upon details. Details shold depend upon quota actions.

If the English language is not powerful, skip it automatically. This principle means that the Code depends on abstraction rather than implementation. What does this mean? I have a metaphor: Do companies need you? Yes. This is called dependency. But do you think your company cannot leave you? Obviously not. companies only rely on people with skills. Therefore, the skilled people here are the abstractions of you and your colleagues, including those in society. What is the so-called inversion? If a company depends on you, it is right. But now the company depends on skilled people and abstract collection, which is called inversion. The positiveness refers to the dependence on specific implementations, while the inversion relies on the abstraction of specific implementations.

Body

1. What are the advantages of dependency inversion?

The dependency inversion principle is dependent on abstraction, so this question is equivalent to asking, What are the advantages of abstract programming? This makes it easy to answer questions. What is abstraction? Abstract refers to the classification of a group of individuals with similar features and behaviors. In this way, I don't need to customize functions for each individual class. Imagine that when I add a function named "kill chicken", its parameter is not an abstract chicken, but a chicken. So when I want to kill the chicken, you also need to reload a function whose parameter is feed chicken. This will be very troublesome. Therefore, one of the advantages of dependency inversion is:

You do not have to customize special processing functions for a specific individual to avoid a large number of boring function overloading.

Are there other benefits? Of course, dependency inversion can reduce coupling between specific classes. What does this mean? Look at the following bad program:

 1 class QiRuiQQ{ 2 public: 3     void run(){ 4         cout<<"QiRuiQQ running..."<<endl; 5     } 6 }; 7  8 class Driver{ 9 public:10     void drive(Benz bz){11         bz.run();12     }13 };14 15 int main(){16 17     Driver *d = new Driver();18     QiRuiQQ *q = new QiRuiQQ();19     d->drive(*q);20 21     delete q;22     delete d;23 24     return 0;25 }

What are the coupling degrees of these two classes? It's too high. Why? Imagine what if the driver wants to drive Ferrari? I now have a Ferrari class:

1 class Ferrari{2 public:3     void run(){4         cout<<"Ferrari running..."<<endl;5     }6 };

The driver could only watch such a super sports car, but he could not drive it. To enable the driver to drive Ferrari, we need to add an overload function for drive, which is similar to qiruiqq's drive method. So what should we do? At this time, we need a great Dependency inversion principle. Let's look at the code that conforms to the dependency inversion:

1/** 2 * vehicle abstraction 3 **/4 class ICAR {5 public: 6 virtual void run () {} 7 }; 8/** 9 * driver abstraction 10 **/11 class idriver {12 public: 13 virtual void drive (ICAR car) {} 14 }; 15/** 16 * vehicles: Chery qq17 **/18 class qiruiqq: Public ICAR {19 public: 20 void run () {21 cout <"qiruiqq running... "<Endl; 22} 23}; 24/** 25 * vehicles: Ferrari 26 **/27 class Ferrari: Public ICAR {28 public: 29 void run () {30 cout <"Ferrari running... "<Endl; 31} 32}; 33/** 34 * typical driver 35 **/36 class driver: Public idriver {37 public: 38 void drive (ICAR * car) {39 car-> Run (); 40} 41}; 42 43 int main () {44 45 driver * D = new driver (); 46 qiruiqq * q = new qiruiqq (); 47 d-> drive (Q); 48 49 Delete Q; 50 delete D; 51 system ("pause"); 52 return 0; 53}

Running result:

Qiruiqq running...
Press any key to continue...

 

At this time, no matter what car you give the driver, the driver will drive, as long as the car inherits ICAR. This reduces the coupling between the two categories. Therefore, the second benefit of dependency inversion comes out:

Reduce the coupling between classes.

2. What are the dependency implementation methods?

The so-called dependency implementation seems awesome. In fact, it is very simple, that is, how to pass parameters to another class. The author provides three methods. Let's see which three methods are used.

◇ A. Constructor (input another object when constructing an object)

◇ B. setter method (Java contains the bean concept, and C ++ can completely imitate it! It is actually a function for setting the value of the member attribute)

◇ C. interface declaration dependent object (that is, the code above)

Summary

1. The dependency inversion principle can avoid a large number of function overloading.

2. Dependency inversion can reduce the coupling between classes.

3. There are three dependency implementation methods: constructor, setter method, and interface declaration dependent object.

Note:

Although the Dependency inversion provides a convenient way for us, we can see from the code above that it is easy to expand the scale of the class. If you declare an interface for each class, it will be terrible to expand. Therefore, for our technical personnel, there must be something to grasp in the application of technology.

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.