Đang chuẩn bị liên kết để tải về tài liệu:
Design Patterns FOR Dummies phần 7
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Phương pháp mô hình mẫu, và mô hình Builder. Phương pháp mô hình mẫu cho phép lớp con định nghĩa lại các bước liên quan trong việc tạo ra một đối tượng, đó là sẽ có ích ở đây khi đó là thời gian để tạo ra các loại robot khác nhau. Và các mô hình Builder cung cấp cho bạn tính linh hoạt nhiều hơn | Chapter 8 Handling Collections with the Iterator and Composite Patterns 179 In other words iterators are designed to let you handle many different kinds of collections by accessing their members in a standard accepted way without having to know the internal details of those collections. The Iterator design pattern is especially important when the collection you re creating is made up internally of separate subcollections as when you ve mixed hashes with array lists for example. Iterators are usually written in Java as standalone classes. Why aren t iterators built into the collections they work with They could be but in Java and other languages they re not. The design insight here is one of what s called single responsibility a class should have only one thing to do. The thinking is that the collection maintains the collection the iterator provides access to the elements of the collection. Separating responsibilities between classes is useful when a class has to change if there s too much going on in a single class it s going to be too hard to rewrite. When change has to happen a single class should have only one reason to change. Accessing your objects with an iterator You start to work on the CEO s problem of tracking vice presidents. In this case you decide to store the vice presidents in a collection with a set of methods that provide access to the VPs. In the early days of iterators the classic methods that iterators were supposed to implement were the following was first next 1 isDone currentitem In Java today however iterators follow the lead of the java.util.Iterator interface which defines these three methods next hasNext remove 180 Part II Becoming an OOP Master The next method returns the next element in the collection hasNext returns true if there are additional elements in the collection and false otherwise and remove allows you to remove an element from the collection. That s how iterators work they provide a .