Đang chuẩn bị liên kết để tải về tài liệu:
Interface-Oriented Design phần 5

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Nhưng hãy để tôi cung cấp cho bạn một từ lập trình. Đây là những ngôn ngữ lập trình Fortran, C, C + + và Java. Làm thế nào nhiều người trong số các ngôn ngữ này liên quan đến nhau? a), b) Ba, c.) Bốn, d) Không Nếu bạn và nhóm của bạn đồng ý trên một câu trả lời, sau đó bạn có thể chia sẻ một cách tiếp cận chung tới sự gắn kết. | An Interface Alternative for Inputstream 76 using inheritance Advantage base classes can provide common implementations Disadvantage difficult to adapt to new situations. 5.4 An Interface Alternative for Inputstream What do football players mammals and geometric shapes have in common We ve used them as examples to show the differences between inheritance and interfaces. Let s look at a real-life class hierarchy and see how an alternative organization with interfaces would appear. This is a concrete example of the concepts discussed in the previous sections. Java s java.io.InputStream class is an abstract class.8 Inputstream contains many methods defined as abstract such as read . Other methods are concrete but contain this statement in the documentation This method should be overridden by subclasses. a few other methods only suggest that they should be overridden. For example a method that reads an array of bytes is provided. Its code simply calls the read method for each byte but the documentation suggests that a concrete implementation could code this better. many methods in the class have an implementation that does nothing e.g. close .9 To contrast an inheritance approach with an interface approach in a real-code example we will transform the InputStream hierarchy into an interface-based design. This transformation follows the concepts of the Replace Inheritance with Delegation refactoring.10 Inputstream Interface Suppose we have a Custominputstream we developed by inheriting from Inputstream such as in Figure 5.5 on the following page. We start our transformation by extracting an interface from the current methods of the abstract Inputstream class 8See http www.docjar.com html api java io InputStream.java.html. 9This discussion ignores the additional methods that Inputstream inherits from the Object. Inputstream does not override any of those methods. Any implementation of the Inputstream interface will also inherit from Object and thus have those same .

TÀI LIỆU LIÊN QUAN