Effective Java Programming Language Guide phần 8

Hãy xem xét những hậu quả thực hiện các quyết định thiết kế API của bạn. Làm cho một loại công cộng có thể thay đổi đòi hỏi phải có rất nhiều sao chép phòng thủ không cần thiết (khoản 24). Tương tự như vậy, sử dụng thừa kế trong một lớp học công cộng, nếu có thành phần sẽ có được mối quan hệ thích hợp lớp mãi mãi để siêu lớp của nó, | Effective Java Programming Language Guide Consider the performance consequences of your API design decisions. Making a public type mutable may require a lot of needless defensive copying Item 24 . Similarly using inheritance in a public class where composition would have been appropriate ties the class forever to its superclass which can place artificial limits on the performance of the subclass Item 14 . As a final example using an implementation type rather than an interface in an API ties you to a specific implementation even though faster implementations may be written in the future Item 34 . The effects of API design on performance are very real. Consider the getsize method in the class. The decision that this performance-critical method was to return a Dimension instance coupled with the decision that Dimension instances are mutable forces any implementation of this method to allocate a new Dimension instance on every invocation. Even though as of release allocating small objects is relatively inexpensive allocating millions of objects needlessly can do real harm to performance. In this case several alternatives existed. Ideally Dimension should have been immutable Item 13 alternatively the getsize method could have been replaced by two methods returning the individual primitive components of a Dimension object. In fact two such methods were added to the Component API in the release for performance reasons. Preexisting client code however still uses the getsize method and still suffers the performance consequences of the original API design decisions. Luckily it is generally the case that good API design is consistent with good performance. It is a very bad idea to warp an API to achieve good performance. The performance issue that caused you to warp the API may go away in a future release of the platform or other underlying software but the warped API and the support headaches that it causes will be with you for life. Once you ve .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
35    318    1    29-06-2024
163    86    2    29-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.