Jams Gosling (father of Java) said, "To discard classes", of course, it means that it is not to abandon the class itself, but to inherit (extends relationship ). It is better to replace extends with an interface (implements relationship. We should avoid inheritance as much as possible for the following reasons:
1. Without flexibility, using a specific class will cause trouble for the underlying modification.
2. Coupling refers to a measurement in which two entities depend on each other.ProgramEvery day (consciously or unconsciously), Members make decisions that affect coupling: Class coupling, API coupling, application coupling, and so on. In an extends inheritance implementation system, the derived class is very closely coupled with the base class, and this close connection may not be expected. For example, B extends a. When B does not use all the methods in A, the method called by B may produce errors!
We must objectively evaluate the coupling degree, and the system cannot always be loosely coupled, so nothing can be done. What is the basis for determining the degree of coupling? Simply put, the coupling degree is determined based on the stability of the requirement. For those with high stability requirements that are not easy to change, we can design various types into tightly coupled ones (although we discuss the coupling between classes, but in fact, the coupling between function blocks, modules, and packages is also the same), because this can improve efficiency, and we can also use some better technologies to improve efficiency or simplifyCodeFor example, Java internal technology. However, if the demand is very likely to change, we need to fully consider the coupling problem between classes. We can come up with various ways to reduce the coupling degree, but in summary, the abstract hierarchy can be a specific class, an interface, or a group of classes (such as beans ). We can use one sentence in Java to summarize the idea of reducing Coupling Degree: "For interface programming, rather than for implementation programming.
When coding, we will leave our fingerprints, such as the number of public items and the code format. We can coupling metrics to evaluate the risk of re-building code. Because re-building is actually a form of maintaining the code, the troubles encountered during maintenance will also be encountered during re-building. We know that most of the most common random bugs after rebuilding are caused by improper coupling.
The greater the unstable factor, the greater the coupling degree. The greater the unstable factor, the greater the coupling degree.
Certain types of instability = number of dependent classes/number of dependent classes
Number of dependent classes = Total number of other classes compiled during compilation
One idea of how to split a large system into small systems to solve this problem is to combine many classes into a higher level unit to form a high cohesion and low coupling class set, this is an issue that should be taken into consideration during our design process!
The goal of coupling is to maintain the one-way dependency, and sometimes we need to use bad coupling. In this case, you should carefully record the cause to help users of the code later understand the real cause of coupling.