A bad idea of multi-inheritance is wrong-assessment

Source: Internet
Author: User

First, the inventor of a language generally writes something about his own language that is worth reading. You may be able to learn a lot from other cool people, but from the language inventor, you can learn the original design intention of the language inventor and some choices in the design. this idea is unique and unique. so when I learn a new language, if the inventor of the language has written a book, I must read it first.

There are two types of books written by the Language inventor. One is the language teaching material. This is almost a convention, because when a language is used by no one else at the beginning, the language inventor does not teach others how to use it, so no one may use it. The quality of their teaching material may even determine how popular their language will last. I have heard of an interesting remark that the reason why C language is so popular is that C programming language is a very good textbook.
Although exaggerated, it is not without reason. This is the same as the fact that some open-source libraries have high requirements on documents. Is the document written in an open-source library good, whether or not this library will be used by many people has a great relationship. of course, there are also counterexamples. The ghost knows how Anders hejlsberg uses a C # language textbook C # programming language,
It is written like a language specification.
In addition to teaching materials, you can write less books about your own language design ideas. For now, I have only seen the design and evolution of the C ++ language written by Bjarne stroustrup and the simple program world.
You are welcome to recommend other similar books to me. The design and evolution of C ++ language tells the various choices BS made when designing C ++,
After reading this book, I first understood how C ++ was designed to be so difficult to use. Of course, I also understood how to use C ++ correctly. later, I read it several times. After work, I strongly recommended it to C ++ colleagues. In fact, now the book I bought is still in my original colleague, although I have left the original company. if, at that time, the design and evolution of the C ++ language were still too ignorant (I was just taking part in the work at that time ),
In addition, C ++ is the first language I have come into contact with programming. As a result, I like some things that I may be accustomed, I have been working for five years,
I have learned more or less than 10 programming languages, but I still can give me a lot of inspiration, which is even more worthy of recommendation.

  • Ruby Inheritance System

    • Mix-in

      • Stream structure of Ruby
      • Stream structure of C ++
  • Insufficient books
  • Additional Statement
Ruby Inheritance System

A bad idea of multi-inheritance is wrong.
-Songben xinghong

This is what I feel is the most exciting part of the book. This article also focuses on this part, especially the description of the Multi-inheritance part, which fully reflects the knowledge of a good programmer and language designer.

Here, I want to talk about the history of my understanding of this part.
In the design and evolution of the C ++ language,
We know that multi-inheritance is controversial before joining C ++, but BS sticks to its own opinions and eventually C ++ supports multi-inheritance. in addition, he defended the multi-inheritance criticized by many people. He did admit the confusion that the multi-inheritance may bring. However, he believed that in some cases, the multi-inheritance abstraction is indeed meaningful, in addition, this abstraction is sometimes more elegant. based on the design principles of C/C ++, "programmers are always right" and programmers should not be limited manually. BS insists on providing the tool of Multi-inheritance, and let programmers decide when to use will lead to confusion, when to use can make the design more beautiful.
After becoming a standard, the disputes over multi-inheritance have not disappeared. Various languages that support multiple inheritance even have different solutions to solve the problem of diamond inheritance. for example, in C ++, the diamond inheritance also introduces a more distorted virtual inheritance,
Python supports multiple types of inheritance, and because all objects inherit from the common base class objects, any multi-inheritance is actually a diamond inheritance. When you really start to use multiple inheritance on your own, actually, the inheritance structure is already a network. In this regard, Guido van Rossum's answer is to use deep-first searches to determine the order of the base classes. I personally think this problem has not been solved, it is even more amazing.
Countless Java users regard the multi-inheritance of c ++ as the root of jokes and C ++ chaos. They are proud to announce that Java has no such problem. james Gosling provides his own solution, that is, there is no free multi-inheritance without C ++, only single inheritance for implementation is supported, and an interface is introduced, only multi-inheritance of interfaces is allowed. in addition, there are some special differences between the inheritance interface and the inheritance base class. In reality, many people do not call this multi-inheritance and claim that Java has no multi-inheritance.
Interestingly, Old Man Gao went to Google later, and I tried to follow the standard Google best when using C ++.
The C ++ Style Guide clearly stipulates that multiple inheritance of C ++ is prohibited (it does not mean that these two things have any relationship ), in addition, their interface definitions are provided in the style guide. Only Java-like interfaces can be used for multi-inheritance. this is really an interesting thing. In a style guide, forcing to modify a language feature proves how notorious the multi-inheritance of c ++ is.

I personally feel that a previously developed iOS game project was only developed for half a year, because the code was messy and many bugs were not maintained. The project was once cancel in another studio, then we take over the development in the middle and regard the multi-inheritance as the root cause of chaos. Later, most of the work of code sorting was to replace the chaotic inheritance with components, after the improvements, the design was much clearer. Later, when I developed an android project, I used Java in the project for the first time. I deeply realized the limitations of Java on inheritance, it is also true that the design principle "combination is better than inheritance" that gof mentioned earlier in the design pattern is put into practice,
I also learned the interface-Oriented Programming advocated by the Java Community.
As a matter of fact, I gradually realized that "inheritance itself is a kind of strong coupling", that is, the dependency coupling between subclass and parent classes. As mentioned in some books, even inheritance itself is not advocate, that is, it advocates object-based design (OB), rather than object-oriented design. (oo)

The story here is my understanding. Since my understanding is based on various buzzwords and classic books mentioned above, I think most people may have similar opinions with me. but Mats does not.

Mix-in

It was originally intended to share code across inheritance layers, but now another independent object needs to be generated, and each method call must be forwarded to that object, which is not reasonable, the execution efficiency is not high.

Mats directly said that the solution of Multi-inheritance in Java is not convenient and reasonable. The solution provided by Mats is mix-in.
Mix-in limits multiple inheritance according to the following rules.
General inheritance uses a single inheritance
The second and more than two parent classes must be abstract classes of mix-in.

The mix-in class is an abstract class with the following features.
Instances cannot be generated separately.
Cannot inherit common classes

Through this compromise between interfaces and common multi-inheritance, mix-In provides multi-inheritance for implementation and limits it so that the inheritance structure will not become a diamond, the tree is the same as a single inheritance. in Ruby, mix-in is implemented through the concept of modules. as an example, matx uses the stream implementation example in ruby.

Stream structure of Ruby

Stream structure of C ++

From this point of view, why do I say this is a compromise, because the implementation is more convenient than only allowing multi-inheritance of interfaces, but for real object-oriented, after all, you still need to split some implementations into more fine-grained classes to meet the mix-in requirements. for example, you can see from the above two figures that C ++ requires fewer classes than Ruby requires for the same function. from this point, Mats criticized Java's combination method for generating an independent object to share code, in fact, Max-In requires more independent objects than multi-inheritance, but uses not combination, but inheritance. of course, such restrictions can lead to better design similar to Java,
It avoids diamond inheritance and similar complex inheritance systems. At the same time, it is more convenient than Java. We all know that adding an inheritance only requires one word and adding a combination of objects for calling, we usually need to add n function interfaces and N calls. (although there is a kind of thing called delegation) the interesting thing is that, in programming, the compromise method is often an excellent method...

Insufficient books
  1. The author introduced in the preface that it was edited and modified on the basis of the articles serialized in the magazine. In some cases, we can actually find obvious duplicates, not just text content duplication, the table that even has enumerable already exists.
  2. The subsequent chapters are more interesting, but I feel a little less ideological. so I saw it faster, but this is also wise, and it does not rule out my lack of interest in the relevant content.
Additional Statement

In my latest article, I changed all the links to books from Douban to Amazon. If you click my link and buy a book, Amazon will give me some commission, of course, I know this is not much, but it is better than nothing. later articles will be similar and I will not declare it again. I don't even know why such harmless things need to be declared, but it seems that you need to do so in China, because others have done so.

Writen by nine days Yan Ling (www.jtianling.com)

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.