Tại sao danh sách thứ hai có Danh sách được sử dụng thay vì ArrayList?
Đó là một thực hành tốt: Chương trình để giao tiếp chứ không phải là thực hiện
Bằng cách thay thế ArrayList
với List
, bạn có thể thay đổi List
thực hiện trong thời gian tới như sau tùy thuộc vào trường hợp sử dụng doanh nghiệp của bạn.
List<Object> list = new LinkedList<Object>();
/* Doubly-linked list implementation of the List and Deque interfaces.
Implements all optional list operations, and permits all elements (including null).*/
HOẶC
List<Object> list = new CopyOnWriteArrayList<Object>();
/* A thread-safe variant of ArrayList in which all mutative operations
(add, set, and so on) are implemented by making a fresh copy of the underlying array.*/
HOẶC
List<Object> list = new Stack<Object>();
/* The Stack class represents a last-in-first-out (LIFO) stack of objects.*/
HOẶC
một số khác List
thực hiện cụ thể.
List
giao diện xác định hợp đồng và thực hiện cụ thể List
có thể thay đổi. Theo cách này, giao diện và triển khai được kết hợp lỏng lẻo.
liên quan SE câu hỏi:
What does it mean to "program to an interface"?
Nguồn
2016-12-27 14:35:45
Xem thêm http: // stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-giao diện – Raedwald