We know the difference abstract class and interface definitions in the syntax and programming point of view, the distinction 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 classes in the concept of should be essentially the same (Ref. 銆?銆?in on "is a" relationship at length 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 this 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 abstraction of a type ways were defined as follows:
Use abstract class as defined Door:
abstract class Door (abstract void open (); abstract void close ();)
Use the interface as defined Door:
interface Door (void open (); void close ();)
Other specific Door types can use the abstract class approach extends the definition of the Door or implements to use interface define the 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, mainly to demonstrate abstract class and interface design concept reflected in the difference, independent of other aspects of the issue have done Jianhua, or ignore)? Below Luo Lie Chu possible solutions, and from the design level of these different options for analysis.
Solution 1:
Simple increase in the definition of a Door 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 ();)
Then an alarm of AlarmDoor 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" behavior methods mixed together . This problem is caused by those who depend only on the Door module concept for "alarm" the concept of change (such as: Modify alarm method parameters) change, and vice versa.
Solution 2:
Since the open, close, and 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 means, then reflects two things:
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.
If we understand the problem domain is: AlarmDoor concept of essence in the Door, while it has an alarm function. How are we to design, implementation to reflect specific we mean? Said before, 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 reflected in the concept of problem areas for the understanding of the essence reflects the design intent is correct, reasonable, because they Biaoxian the concept of the relationship between the Bu Tong (Suiran will be realized demand function). Actually, this is a language of idioms, I hope readers will feel for a friend.
Abstract class and interface for the essential difference between the contents of the will to introduce to you here, hope you understand and learn abstract class and interface help.
相关链接:
XviD to MP4
British GRAMS, Novel Medicine 1000 Chain layout help
Notebook computer repair experience hidden rules
Types of scanners find different types of mini
MPEG to 3GP
Analysys on Yang: Adjustment for a number of ideas through high-level
Good File And Disk Management
AVI to MPEG4
Taught YOU how to DB2 patch
DDos remains a primary threat to a number Of ISP
Business Databases And Tools evaluation
Students This Year, Real Salary Levels Jiemi Fresh
Good Graphic
Regional Manager how to handle high-level interaction with the company
U disk to create running Win98 super mini
Community 2.0 My Community I CALL the shots
Recreation Catalogs
No comments:
Post a Comment