Why is it harmful for Java to inherit most of them _ MySQL

Source: Internet
Author: User
Most good designers avoid using implementation inheritance (extends relationships) just as they do without plague ). In fact, 80% of the code should be completely written using interfaces instead of extends. The book "Java Design Patterns" describes in detail how to use interface inheritance to replace implementation inheritance. This article describes why the designer did this. Extends is harmful; maybe for CharlesM

Most good designers avoid using implementation inheritance (extends relationships) just as they do without plague ). In fact, 80% of the code should be completely written using interfaces instead of extends. The book "Java Design Patterns" describes in detail how to use interface inheritance to replace implementation inheritance. This article describes why the designer did this.

Extends is harmful; it may not be for Charles Manson, but should be avoided whenever possible. The book "JAVA Design Patterns" has spent a lot of time discussing how to use interface inheritance instead of implementing inheritance.

A good designer mostly uses interfaces in his code, rather than a specific base class. This article discusses why designers choose this and also introduces some interface-based programming basics.

  Interface and Class )?

Once, I attended a Java User Group Meeting. During the meeting, Jams Gosling (father of Java) made a speech by the initiator. In that memorable Q & A section, someone asked him: "What do you want to change if you reconstruct Java ?". "I want to discard classes" he replied. After the laughter subsided, it explained that the real problem was not because of the class itself, but the implementation of the extends Relationship. The implements relationship is better. You should avoid inheritance as much as possible.

Lost flexibility

Why should you avoid inheritance? The first problem is that you use specific class names to fix specific implementations and add unnecessary difficulties to underlying changes.

In the current agile programming method, the core is the concept of parallel design and development. Before you design the program in detail, you start programming. This technology is different from the traditional method-the traditional method is that the design should be completed before coding starts-but many successful projects have proved that you can develop high-quality code more quickly, compared with the traditional step-by-step method. However, at the core of parallel development, flexibility is advocated. You have to write your code in a certain way, so that the newly discovered requirements can be easily merged into the existing code as much as possible.

It is better than implementing the features you may need. you only need to implement the features you need clearly, and be moderately tolerant of changes. Without such flexible and parallel development, it would be impossible.

Inteface programming is the core of flexible structure. To explain why, let's take a look at what happens when they are used. Consider the following code:

F ()
{
Jsonlist list = new jsonlist ();
//...
G (list );
}

G (sort list)
{
List. add (...);
G2 (list)
}

Assume that a demand for fast query is raised, so that this sort list cannot be solved. You need to replace it with HashSet. In existing code, changes cannot be localized, because you not only need to modify f (), but also need to modify g () (it has the argument list parameter), and g () any code that passes the list. Rewrite the code as follows:

F ()
{
Collection list = new vertex list ();
//...
G (list );
}

G (Collection list)
{
List. add (...);
G2 (list)
}

In this way, the Linked list can be modified to hash, but new HashSet () can be simply used to replace new sorted list (). That's it. There are no other places to modify.

As another example, compare the following two pieces of code:

F ()
{
Collection c = new HashSet ();
//...
G (c );
}

G (Collection c)
{
For (Iterator I = c. iterator (); I. hasNext ())
Do_something_with (I. next ());
}

And

F2 ()
{
Collection c = new HashSet ();
//...
G2 (c. iterator ());
}

G2 (Iterator I)
{
While (I. hasNext ())
Do_something_with (I. next ());
}


The g2 () method can now traverse the derivation of Collection, just like the key-value pair you can get from Map. In fact, you can write iterator, which generates data instead of traversing a Collection. You can write iterator, which obtains information from the framework or file of the test. This provides great flexibility.

  Coupling

For implementation inheritance, a more critical issue is coupling-annoying dependency, which is the dependency of one part of the program on another part. Global variables provide a classic example to illustrate why strong coupling causes problems. For example, if you change the type of a global variable, all functions that use this variable may be affected, so all the code will be checked, changed, and retested. In addition, all functions that use this variable are coupled with each other through this variable. That is, if a variable value is changed when it is difficult to use, one function may not affect the behavior of another function correctly. This problem is significantly hidden in multi-threaded programs.

As a designer, you should try to minimize coupling. You cannot eliminate coupling together, because the method call from an object of a class to an object of another class is in the form of loose coupling. You cannot have a program without any coupling. However, you can minimize coupling by following OO rules (the most important thing is that the implementation of an object should be completely hidden in the use of its objects ). For example, the instance variable of an object (not a member field of a constant) should always be private. I mean in a certain period of time, there are no exceptions and constant. (You can occasionally use the protected method effectively, but the protected instance variable is an annoying thing) for the same reason, you should not use the get/set function-they only make people feel too complicated for a public domain (although the access function that returns the modified object instead of the basic type value is in in some cases, the reason is, in that case, the returned object class is a key abstraction during design ).

Here, I am not angry with the book. In my own work, I found a direct relationship between my OO methods, fast code development and easy code implementation. Whenever I violate the center's OO principles, such as implementation hiding, I overwrite the code in the result (generally because the code cannot be debugged ). I don't have time to rewrite the code, so I follow those rules. What do I care about? I am not interested in the reason for cleaning.

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.