Someone asked jams Gosling, father of Java: "If you reconstruct Java, what do you want to change ?". "I want to discard class" he replied. Laughter subsided
He explained, "the real problem is not because of the class itself, but because of the extends. the better choice is the implements )."
Nowadays, more and more people are calling extends for its shortcomings. in Java design patterns, this article details how to use interface inheritance to replace implementation inheritance. So why? One of the several features of object-oriented programming is inheritance. Today, why are designers hiding extends like a plague? In the final analysis, it is still a [coupling] problem. Inheritance makes the subclass closely dependent on the parent class, and the entire structure loses flexibility, and the parent class cannot be modified freely.
Coupling is the dependency of a part of the Program on another part. Although
However, coupling cannot be eliminated, but coupling should be minimized. No program contains no dependency. Even in IOC, dependency exists, but the dependency is transferred to the configuration.
Already. However, the coupling degree of a good program is often far lower than that of a bad program. Everyone understands this truth, but in practical application, we are often forced to cope with schedule pressure, instead of worrying about loose coupling.
Focus on business. But the final result is often the opposite. The more we sacrifice the program structure to catch up with the progress, the more time the schedule lags behind.
Inheritance makes the structure less flexible. You can use a specific class name to fix the specific implementation, which adds unnecessary difficulties to the underlying changes. In Agile development, the core is the concept of parallel design and development. In the detailed design process
We started programming. This is different from the traditional development form (encoding starts only after the design is complete ). The core of parallel development is to claim flexibility. We must ensure the flexibility of the program structure in some way
Meet the changing needs. In this situation, the advantages of using interfaces are obvious, and the disadvantages of using inheritance are also obvious.
In the inheritance system, modifying the base class is very tricky, because the derived class is closely dependent on the base class, any changes to the base class may lead to the loss or disorder of the function of the derived class. We can't simply determine whether the changes to the base class are safe, so we have to re-test all the derived classes. In addition, when modifying the base class, we must be aware of any simple changes to the base class.
The entire program may not be operated.
This article from http://blog.sina.com.cn/jbwang