Design: When do Java interfaces and abstract classes work? How to use it?

Source: Internet
Author: User

I made a mistake today:

" interface changes, hurt –, unless you're sure you're the only one who's using it." Even if just throw a new exception. Haha, this is my mistake.

I. Interfaces and abstract classes

Class, which is an object .

First, abstract class , is abstract out of the basic part of the class, that is, abstract base class (abstract Class). The official definition is confusing, but the memory method is also good-the class that contains the abstract method is called the abstract class.

An interface is a deeper depth of abstraction, which is like expressing a relationship between classes with a few rules that are short and non-logical. Can be likened to protocols, such as the UDP/TCP protocol used for communication.

Summary : classes and interfaces are the basic abstract units of the Java language.

Second, why there are two main reasons for the interface

A. transition up to multiple base types

This will provide considerable flexibility for development. such as the goddess Liu Yifei (Class), the realization of the star and woman interface. This is used in a class of complex inheritance structures, and later when calling the Seestar (Star Star) or seewomen (women women) method, simply passing in its implementation class (Liu Yifei). This is often said that the interface can be multi-implementation , to achieve a complete decoupling .

B. reusability

that is, according to the interface definition, let the creation class have a "protocol" (rule) to follow. Whatever~ to do is to create only one interface, in order to guarantee the non-coupling of the generated object. In this way, the use of interfaces makes the code more reusable, versatile , and flexible . But not so omnipotent. The following code will be used.

third, how to use?

Former NPC Bull summed up some design patterns , that is, the interface derived from some design patterns. The design pattern is the sweetness of the grammatical sugar. The interface gives us a taste of sweetness. Like a cup of Starbucks hot chocolate around me. It's a little too sweet. Like what:

A. policy mode -the parameters in the method use an interface, and the passed-in Parameter object (the implementation class) contains the code that executes .


The invocation process is as follows, in the method of access implementation only:

B. Adapter mode - interface Adapter (Interface Adapter) class that can be used to match different sources to the same destination . That is, exposing the target interface and the implementation source have a common method, adapter class How to fit it? Implementation of the target interface, and associated with the implementation of the source object, in the implementation method calls the association to implement the source real object, and then carry out a variety of adaptation operations. such as associating a source or something.

This is actually a bit of an AOP flavor. For example, the Spring AOP Framework's support for Beforeadvice, Afteradvice, throwsadvice three notification types is actually implemented with adapter mode .

C. Factory mode -the factory object will generate an object for an implementation of the interface . The code then separates the implementation from the implementation of the interface and transparently replaces one implementation transparently with another. But the factory call method here is static, which is the Simple Factory mode (static Factory mode). Dynamic Factory mode is nothing more than using reflection to reach a dynamic call.

IV. Guidelines for the use of interfaces and abstract classes

First, try to make every class or member non-accessible to the outside world

There is a degree in the outside world, such as the package level or the public. This can be better modular , between modules and modules through exposed API transfer. This way, if a module changes the interface or class. Just worry about the module and not the other modules involved.

Second, appropriate use of the class (abstract class) inheritance, more use of composite

inheritance, which implements code reuse. It is very safe to use inheritance internally , but remember when to use inheritance. That is, when a subclass is really a subtype of a superclass , inheritance is applied. Otherwise, use compositing whenever possible, that is, referencing an instance of another class in one class . That means wrapping another class, which is what the adornment pattern embodies.

Third, priority is given to using interfaces, compared to abstract classes

First Java only permits single inheritance , which causes the abstract class definition to receive great restrictions. Both, the interface cannot implement the method . But Java 8 provides a functional interface .

However, when designing the interface, it is important to be careful about designing public interfaces . If the interface is publicly released, it will certainly be widely implemented, so it is almost impossible to change the interface, it will be a huge project. (This is the same as the mistake I made.) )

Four, the occupied time did not have four ...

Summary:

Understand when Java interfaces and abstract classes are used? How to use it? To be continued, there is a new point to add.

Welcome to click on my blog and github-blog to provide RSS Subscription Oh!

———- http://www.bysocket.com/ ————- https://github.com/JeffLi1993 ———-

Micro-Blog: Bysocket Watercress: Bysocket FaceBook: Bysocket Twitter: Bysocket

Design: When do Java interfaces and abstract classes work? How to use it?

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.