顥?顥?abstract class and interface is an abstract class for the Java language to support definition of the two mechanisms, precisely because of the existence of two mechanisms, it gives a powerful object-oriented Java ability. between abstract class and interface support for the abstract class definition has a very similar, even interchangeable, so many developers making the definition of an abstract class abstract class and interface for the choice was relatively arbitrary. In fact, between the two still have great differences, even for their choice reflects the understanding of the nature of the problem areas, for the understanding of design intent is correct and reasonable. This paper will be some differences between them analyzed, trying to provide developers with a basis to choose between the two.
顥?顥?understand the abstract class
顥?顥?abstract class and interface in the Java language are used for abstract class (abstract class in this article is not translated from the abstract class from which that is an abstract body, while abstract class for the Java language used to define the abstract class method, the reader is a distinction) is defined, then what is an abstract class, use the abstract class can bring us any good?
顥?顥?In object-oriented concept, we know that all of the objects are described by class, but in turn, is not the case. Not all classes are used to describe the object, if a class does not contain sufficient information to describe a specific object, so the class is an abstract class. Abstract class is often used to characterize the problem areas of our analysis, design, abstract concept derived, is a series of look different, but essentially the same concept of the abstract concrete. For example: If we carry out a graphic editing software, you will find there are problem areas round, triangle such specific concepts, they are different, but they are both belong to the shape of such a concept, the concept of the shape of the field in question is not exist, it is an abstract concept. Because the field of the abstract concept does not correspond to specific concepts, so are used to describe abstract concepts of the abstract class can not be instantiated.
顥?顥?object-oriented field, the abstract class type is mainly used to hide. We can construct a fixed set of abstract description of behavior, but behavior of this group but can have any number of possible concrete implementation. This abstract description is an abstract class, and this group of any number of possible concrete realization of the performance of all possible derived classes. Modules can operate as an abstract body. As the module relies on a fixed abstraction, so it can not allow changes; the same time, derive from this abstraction, but also extend the behavior of functions in this module. Readers familiar with the OCP must know that in order to achieve one of the most object-oriented design core principles OCP (Open-Closed Principle), an abstract class is one of the key.
顥?顥?see from the syntax level, abstract class and interface
顥?顥?In the syntax level, Java language for the abstract class and interface definitions are given different ways to define the following abstract class called Demo example to illustrate this difference.
顥?顥?way of using abstract class abstract class Demo defined as follows:
顥?顥?abstract class Demo (
顥?顥?顥?abstract void method1 ();
顥?顥?顥?abstract void method2 ();
顥?顥?顥?...
顥?顥?
顥?顥?way of using the interface defined Demo abstract class as follows:
顥?顥?interface Demo (
顥?顥?顥?void method1 ();
顥?顥?顥?void method2 ();
顥?顥?顥?...
顥?顥?
顥?顥?In the abstract class in the way, Demo can have their own data members, can also be a member of a non-abstarct method, and in the interface mode of implementation, Demo can only have static data member can not be modified (that is to be static final, but generally not defined in the interface data members), all members of the methods are abstract in. In a sense, interface is a special form of abstract class.
顥?顥?From the programming point of view, abstract class and interface can be used to implement "design by contract" idea. However, the specific use of the above there are some differences.
顥?顥?first, abstract class in the Java language that is a kind of inheritance, a class can only use an inheritance. However, a class but can implement multiple interface. Perhaps this is the Java language, Java designers to consider support for multiple inheritance of a compromise to consider it.
顥?顥?Secondly, in the abstract class definition, we can give the default behavior of methods. But in the interface definition, the method can not have the default behavior, in order to circumvent this restriction, you must use the proxy, but this will add some complexity, sometimes causing serious trouble.
顥?顥?In the abstract class can not define the default behavior There is another more serious problem that may cause maintenance problems. If you later want to change classes because of the interface (usually through the abstract class or interface to that) to adapt to new circumstances (for example, add a new method or methods used to have to add new parameters), they will very much trouble may take a lot of time (for the derived class a lot of cases, particularly). However, if the interface is achieved through the abstract class, so may only need to modify the definition of the abstract class in the default behavior on it.
顥?顥?equally, if not in the abstract class defined in the default behavior, it will lead to the same method to achieve the abstract class in a derived class for each violation of the "one rule, one place" principle, resulting in code duplication, the same not conducive to future maintenance. Therefore, in the abstract class and interface to choose between to be very cautious.
顥?顥?see from the design level, abstract class and interface
顥?顥?above the main syntax and programming from the perspective discusses the difference between abstract class and interface, the difference between these levels is relatively low-level, nonessential. The section from another level: abstract class and interface design reflected, to analyze the difference between the two. The author believes that this level of analysis to understand the essence of both concepts.
顥?顥?As already mentioned, abstarct class in the Java language embodies a succession of relationships, in order to make a reasonable inheritance, the parent class and derived class must exist between the "is a" relationship, that is the parent class and derived class In essence, the concept should be the same (Ref. 銆?銆?in on "is a" relationship of the large size of in-depth discussion, interested readers can refer to). For the interface it is not, does not require interface implementation and interface definition of the concept is essentially the same, only the interface definition of the contract to achieve it. To make discussion easy to understand, following a simple example will be explained.
顥?顥?consider an example, assume that the problems in our area, there is a abstraction on the Door, the Door is open and the implementation of two moves close, then we can define abstract class or interface that the abstract concept of a type of approach were defined as follows:
顥?顥?way to use the definition of abstract class Door:
顥?顥?abstract class Door (
顥?顥?顥?abstract void open ();
顥?顥?顥?abstract void close ();
顥?顥?
顥?顥?use interface as defined Door:
顥?顥?interface Door (
顥?顥?顥?void open ();
顥?顥?顥?void close ();
顥?顥?
顥?顥?Door other specific types can use the abstract class approach extends the definition of the Door, or implements methods to use interface defined Door. Appear to use the abstract class and interface is not a big difference.
顥?顥?If you are asking the Door also has alarm function. How do we design a class structure for the example of it (in this case, primarily to display abstract class and interface design concept is reflected in the difference, other issues have done nothing to simplify or ignore)? The following will set out the possible solutions, and from the design level of these different options for analysis.
顥?顥?Solution 1:
顥?顥?simple increase of the Door in the definition of an alarm method, as follows:
顥?顥?abstract class Door (
顥?顥?顥?abstract void open ();
顥?顥?顥?abstract void close ();
顥?顥?顥?abstract void alarm ();
顥?顥?
顥?顥?or
顥?顥?interface Door (
顥?顥?顥?void open ();
顥?顥?顥?void close ();
顥?顥?顥?void alarm ();
顥?顥?
顥?顥?The AlarmDoor then an alarm definition as follows:
顥?顥?class AlarmDoor extends Door (
顥?顥?顥?void open () (...)
顥?顥?顥?void close () (...)
顥?顥?顥?void alarm () (...)
顥?顥?
顥?顥?or
顥?顥?class AlarmDoor implements Door (
顥?顥?顥?void open () (...)
顥?顥?顥?void close () (...)
顥?顥?顥?void alarm () (...)
顥?顥?
顥?顥?This method violates the object-oriented design of a core principle of ISP (Interface Segregation Priciple), the definition in Door to Door concept inherent in the concept of behavioral approach and the other "alarm" of the mixed method together. So caused a problem that only depends on the Door module concept because of "warning devices" is the concept of change (姣斿: Modify alarm Fangfa of parameters) changed, otherwise still.
顥?顥?Solution 2:
顥?顥?Since the open, close and the alarm are two different concepts, they should be according to the principle of ISP were representatives of the two concepts defined in the abstract class. Defined methods are: the two concepts are defined using the abstract class methods; the two concepts are defined using the interface means; a concept means using the abstract class definition, another definition of the concept of using the interface mode.
顥?顥?Clearly, Java language does not support multiple inheritance, so the two concepts are defined using the abstract class approach is not feasible. Behind the two methods are feasible, but for their choice is reflected in the concept of problem areas for the understanding of the nature, reflect the design intent is correct and reasonable. We are 11 to analyze, explain.
顥?顥?If the two concepts are defined using the interface method, then reflects two problems: 1, we may not have a clear understanding of problem areas, AlarmDoor the concept of nature in the end is the Door or alarm? 2, if we do not understand the problem domain issues, such as: Through the analysis of the problem areas found AlarmDoor conceptual in nature and Door is the same, then we realized when we failed to reveal the correct design intent, because In the definition of these two concepts (both ways using the interface definition) and do not reflect the meaning.
顥?顥?problem areas if we are to understand that: AlarmDoor concept of essence in the Door, while it has an alarm function. How are we to design, implementation to reflect specific we mean? As already mentioned, abstract class in the Java language that a kind of inheritance, and inheritance in nature "is a" relationship. Therefore, the concept for the Door, we should use abstarct class way to define. In addition, AlarmDoor has an alarm, indicating it can complete the definition of the concept of the behavior of police, so police can interface define the concept. As follows:
顥?顥?abstract class Door (
顥?顥?顥?abstract void open ();
顥?顥?顥?abstract void close ();
顥?顥?
顥?顥?interface Alarm (
顥?顥?顥?void alarm ();
顥?顥?
顥?顥?class AlarmDoor extends Door implements Alarm (
顥?顥?顥?void open () (...)
顥?顥?顥?void close () (...)
顥?顥?顥?void alarm () (...)
顥?顥?
顥?顥?This approach can basically be able to clearly reflects our understanding of the problem areas, the right reveals our design intent. In fact, that is the abstract class "is a" relationship, interface that is "like a" relationship, we can, as in the choice of a basis, of course, is based on the understanding of problem areas, such as: If we that the concept is essentially AlarmDoor alarm function also has the Door, then the way would turn the definition.
顥?顥?Conclusion
顥?顥?abstract class and interface is the Java language in the definition of abstract class of two ways, among them a great similarity. However, the choice for them but often reflect problems in the field of concept for the understanding of the nature, reflect the design intent is correct, reasonable, because they showed the different relationships between concepts (although both can achieve demand function). Actually, this is a language of idioms, I hope readers will feel for a friend.
相关链接:
Infomation Clocks And Alarms
XviD to MP4
Renjiao She Coke Man: step by step
Moving to the countryside to the computer through three banks
changing ACTIVE partition may damage ntldr file in
Hot Shell Tools
I pirated your personal fight Daolian Thunder Sohu
Evaluate Games Simulation
Private hospital, why are white Fujian Youyizuozhuang 80 per?
Some tips on data collection
Infomation Gallery And CATALOGING Tools
Harbin Engineering University, the world's five major Classification societies in hand
The BASICS of intrusion detection rules
After the way you do business you're considering?
M2TS to MKV
MKV to Zune
No comments:
Post a Comment