The quality of a software design depends to a large extent on its overall architecture. The overall architecture is actually your abstract framework for the entire macro business, when the structure of the high-level abstraction layer representing the business logic is reasonable, you only need to consider some algorithms and specific business implementations. When you need to develop another similar project, your previous abstraction layer may be able to be used again. In the face of Object design, the focus of reuse should be the reuse of the abstraction layer, instead of reusing a specific code block, do you suddenly feel that your understanding of reuse has increased? Pai_^
When it comes to abstraction, I can't help but mention the Java interfaces and Java Abstract classes that once gave me a headache. This is also the focus of this article.
Since the focus of object-oriented design lies in abstraction, Java interfaces and Java Abstract classes will inevitably exist.
Java interfaces and Java Abstract classes represent abstract types, which are the specific manifestations of the abstraction layer we need to propose. To improve the Reuse Rate of programs, increase the maintainability and scalability of programs, the OOP object-oriented programming must be interface-oriented and abstract-oriented, correctly Use interfaces and abstract classes as the top layers of your structural hierarchy.
There are too many similarities between Java interfaces and Java Abstract classes, and there are too many special points. What exactly is the best place for Java interfaces and Java Abstract classes? By comparing them, you can find out.
1. The biggest difference between Java interfaces and Java Abstract classes is that Java Abstract classes can provide partial implementation of some methods, but Java interfaces cannot. This is probably the only advantage of Java Abstract classes, however, this advantage is very useful.
For example, if a new method is added to an abstract class, all its subclasses will get this new method at once, and the Java interface cannot do this, if you add a new method to a Java interface, all classes that implement this interface cannot be compiled successfully, because you must make every class implement this method again, this is obviously a disadvantage of the Java interface.
2. The implementation of an abstract class can only be given by the subclass of this abstract class. That is to say, this implementation is in the hierarchy defined by the abstract class. Due to the single inheritance of the Java language, therefore, the efficiency of an abstract class as a type definition tool is greatly reduced.
At this point, the advantages of the Java interface come out. Any class that implements the method specified by a Java interface can have the type of this interface, A class can implement any number of Java interfaces, so this class has multiple types.
3. From the 2nd point, it is not difficult to see that the Java interface is an ideal tool for defining the hybrid type. The mixed class indicates that a class has not only a primary type but also other secondary behaviors.
4. Combined with the advantages of abstract classes and Java interfaces in and, the classic design pattern came out: the Declaration type work is still undertaken by the Java interface, however, a Java Abstract class is provided and this interface is implemented. Other specific classes of the same abstract type can choose to implement this Java interface or inherit this abstract class, that is to say, in the hierarchy, the Java interface is at the top, followed by the abstract class. Ha, the biggest advantages of the two are all brought to the extreme. This mode is the "Default adaptation mode ".
This mode is used in Java APIs and all follow certain naming rules: Abstract + Interface Name.
Java interfaces and Java Abstract classes exist for implementation and Inheritance of specific classes. If you want to write a specific class to inherit another specific class, then there is a big problem with your design. The Java Abstract class exists for inheritance, and its abstract method is to force the subclass to be implemented.
Use Java interfaces and abstract Java classes to declare the types of variables, declare parameters as types, return methods, and convert data types. Instead of using a specific Java class to declare the type of the variable, the parameter is the type declaration, the return type description of the method, and the conversion of the data type.