The interface mode of Java design mode

Source: Internet
Author: User

-----------pattern is the embodiment of thought, not the concrete realization.

Abstract speaking, the interface of a class is a set of methods and fields that the class allows other class objects to access. Interfaces often represent a promise that methods need to implement the operations represented by the interface method name, follow code comments and other documentation instructions, and the implementation of the class is the code in the square body.

 java does not allow multiple inheritance, that is, a subclass can have only one parent class, son extends Fathera,fatherb is wrong in order to compensate for this deficiency, Java allows the implementation of a number of interfaces, interface is to give some no content method, similar to C + + The virtual class in the. To the specific use of the method to define their own content, to be aware of the interface must be implemented to implement all methods of the interface.     in fact, because Java does not support multiple inheritance to produce an interface, the interface is used to standardize the class of it can avoid the inconsistency of the design of the class, which is important in the development of multi-person cooperation, such as the interface has a method, then the implementation of this interface must implement a method, This forms a specification, that is, a inherits B, but wants to use a method of C, but can no longer inherit, so it uses the C interface.     Java interfaces and Java abstract classes represent abstract types, which are the concrete manifestations of the abstraction layer we need to propose. OOP object-oriented programming, if you want to increase the reuse rate of programs, increase the maintainability of the program, extensibility, it must be interface-oriented programming, oriented to abstract programming, the correct use of interfaces, abstract classes, these too useful abstract types as the top layer of your hierarchy.  =============================================================================================      Java interface and Java abstract class there are too many similarities, there are too many special places, where exactly is the best place for them? Compare them and you'll find out.     1, Java interface and Java abstract class The biggest difference is that Java abstract class can provide some of the methods of implementation, and Java interface can not, this is probably the only advantage of Java abstract class, but this advantage is very useful. If you add a new concrete party to an abstract class, all of its subclasses get the new method all of a sudden, and the Java interface does not do this, and if a new method is added to a Java interface, all classes that implement the interface will fail to compile successfully. Because you have to let every class implement this method again, this is obviously a disadvantage of the Java interface. The implementation of     2, an abstract class can only be given by subclasses of this abstract class, that is, the implementation class is in the hierarchy of inheritance defined by the abstract class, and because of the single inheritance of the Java language, the abstract class as a classThe efficiency of the type definition tool is greatly compromised. At this point, the advantages of the Java interface come out,any class that implements a method defined by a Java interface can have a type of this interface, and a class can implement any number of Java interfaces, so that the class has multiple types. 3, from the 2nd is not difficult to see, Java interface is the ideal tool to define a mixed type, mixed class indicates that a class does not only have the behavior of a primary type, but also has other secondary behavior. 4, the combination of 1, 2 points in the abstract class and Java interface of the respective advantages,The most refined design pattern comes out: the work of declaring type is still assumed by the Java interface, but at the same time it gives a Java abstract class, and implements this interface, and other similar to the abstract type of the specific class can choose to implement this Java interface, you can also choose to inherit this abstract class, That is, in the hierarchy, the Java interface at the top, followed by the abstract class, ha, the next two of the greatest advantages can be played to the extreme. This mode is "default adaptation mode". This pattern is used in the Java language API, and all follow a certain naming convention: Abstract + interface name. Java interfaces and Java abstract classes exist to be used for the implementation and inheritance of specific classes, and if you are going to write a specific class to inherit another specific class, then your design has a big problem. The Java abstract class exists for inheritance, and its abstract approach is to force subclasses to be implemented. Use Java interfaces and abstract Java classes for variable type declarations, arguments for type declarations, method return type descriptions, and conversion of data types.    Instead of using specific Java classes to declare variable types, arguments are type declarations, method return type descriptions, and data type conversions. I think if you make up the code with an interface and the abstract class is not, perhaps I can say you do not use any design mode, any design pattern is inseparable from the abstract, and abstract and Java interface and abstract Java class is inseparable.

  Java promotes the interface concept to a separate structure, embodying the separation of the interface (the commitment that the object must follow) and the implementation (how the object fulfills the promise). The Java interface allows multiple classes to provide the same functionality, and also allows a class to implement multiple interfaces at the same time.

 The difference between an abstract class and an interface in Java:

1. A class can implement multiple interfaces, but can inherit up to one abstract class.

2. Abstract classes can contain concrete methods, and all methods of an interface are abstract.

3. A field can be declared and used in an abstract class, but an interface cannot be, but a static final constant can be created.

4. Methods in an abstract class can be public, protected, private, or the default package, and the methods of the interface are public.

5. Abstract classes can define constructors, and interfaces cannot.

The advantage of the Java interface is that it restricts collaboration between objects, which in fact provides greater freedom. Even if the implementation of the class implementing the interface has changed dramatically, the client of the interface can remain unaffected.

Summary:

The power of an interface is that it describes the behavior that it expects and does not expect in class collaboration. Interfaces are very similar to abstract classes, but defining behavior does not provide implementations.

Copyright NOTICE: Welcome reprint, Hope in your reprint at the same time, add the original address, thank you with

The interface mode of Java design mode

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.