Interface is better than abstract class

Interfaces are ideal for defining mixins. In general, mixin is a class, in addition to its "main type",
you can also declare that it provides some optional behavior. For example, Comparable is a type interface that allows a class to declare that its
instances are ordered relative to other comparable objects. Such an interface is called a type because it allows optional functions to be "mixed" to
the main function of the type. Abstract classes cannot be used to define mixed classes because they cannot be loaded into existing classes: a class cannot have
multiple parent classes, and there is no reasonable place to insert a type in the class hierarchy.
Interfaces allow the construction of non-hierarchical types of frameworks. For some types of hierarchical organizations it is a good thing, but not other things
are neatly fall into a strict hierarchy. For example, suppose we have a representative of the singer interface, and another on behalf of composers then
mouth:

public interface Singer {
AudioClip sing(Song s);
} p
ublic interface Songwriter {
Song compose(int chartPosition);
}

  In real life, some singers are also composers. Because we use interfaces instead of abstract classes to define these types, it
is completely allowed for a single class to implement both singer and composer interfaces. In fact, we can define a succession singer and composer third connection
ports, and add the new method is suitable for combination

public interface SingerSongwriter extends Singer, Songwriter {
AudioClip strum();
void actSensitive();
}

  You don't always need this flexibility, but when you do, the interface is a savior. Another approach is to
include a separate class of bloated class hierarchies for each supported combination of attributes. If there are n attributes in the


type system , you may need to support 2n possible combinations. This is called combinatorial explosion ( Combinatorial Explosion ) interfaces to ensure the safety of packaging like pattern, very powerful enhancements made possible (see the first 18 bars). If an abstract class is used to
define the type, then the programmer wants to add functionality and can only inherit. The generated class is weaker and more fragile than the packaging class

There are some limitations to using the default method to provide implementation assistance. Although many interfaces specify the behavior of methods in the Object class
(such as equals and hashCode ), they are not allowed to provide default methods. In addition, interfaces are not allowed to contain instance
properties or non-public static members (except for private static methods). Finally, you cannot add default methods to uncontrolled interfaces


However, you can achieve the class (by providing an abstract framework abstract skeletal Implementation class ) to an interface
, the advantages of abstract classes and interfaces from the combination used. The interface defines the type, which may provide some default methods, and the skeleton implementation
class implements the remaining non-original interface methods on top of the original interface methods. Inherited skeleton achieve most of the work required to achieve a connection
port. This is the template method design pattern [Gamma95] .

Conventionally, such a class is called skeleton AbstractInterface , wherein Interface is implemented in the name of the interface thereof
, he said. For example, the Collections Framework provides a framework implementation to complement each main collection interface:
AbstractCollection , AbstractSet , AbstractList, and AbstractMap . It can be said, they called
for the SkeletalCollection , SkeletalSet , SkeletalList and SkeletalMap makes sense, but
it has now been established conventions of abstraction. If properly designed, skeleton realize (whether alone or abstract class only by default on an interface
composed of law) allows programmers to easily provide their own interface.

The advantage of skeleton implementation classes is that they provide help for all implementations of abstract classes without imposing
strict constraints on abstract classes as type definitions . For most implementers of interfaces with skeleton implementation classes, inheriting this class is the obvious choice, but it is not
required. If a class cannot inherit the skeleton implementation, this class can directly implement the interface. This class still benefits from any
default methods of the interface itself . In addition, the skeleton implementation class can still assist in the implementation of the interface. The class implements the interface method call interface can be forwarded to the following
contains an instance of the class private internal bearing skeleton implementation. This is known in the art and multiple inheritance simulated entry 18 discussed packaging pattern density
cut correlation. It provides many benefits of multiple inheritance while avoiding defects

Guess you like

Origin www.cnblogs.com/lIllIll/p/12695497.html