Love life, enjoy entertainment, focus on technology, welcome to pay attention to Qger, we witness growth together!
What is an iterator pattern?
Official explanation: To access the elements of a aggregate object sequentially without exposing its underlying implementation.
Sequentially accesses the elements of the collection object and does not expose its internal implementation
通俗解释:假设给定一个集合对象,定义一个与之相关联的Iterator(迭代器),该迭代器能够访问集合对象的内部元素,通过迭代的方法能够按照顺序依次访问集合对象的每一个元素。
Why use iterator mode?
- It allows the collection's internal element structure to traverse the collection in case the client is transparent. Ensure the encapsulation, transparency.
- Provides a uniform way to access your data Objects (collections), and you do not need to know the types of data objects.
- Practicality is very strong, has been widely used, and the expansion of a strong, easy to expand their own.
How do I use the iterator pattern?
The UML diagram is as follows:
Each component explains:
Iterator: An abstract iterator that defines the most basic interface for an iterator. such as: Hasnext () Indicates whether the iteration has the next element, and next returns the next element of the iteration.
Aggregate: An abstract collection that defines the basic operating interface of a collection. For example: Add () adds a primary color to the collection, and remove () deletes an element.
Comcreteaggregate: A specific data collection class that defines the structure of the collection elements and details of the operation. It also defines a specific iterator within the class and provides a way to return an iterator object.
Concreteiterator: A concrete iterator, usually defined within an aggregation class, to achieve the purpose of accessing the internal data structure of an aggregate class, and to iterate over the aggregation class elements.
Scope of Use:
When you want to access a collection of objects without exposing their internal representations
When you want to have multiple traversal methods to traverse a collection of objects, such as positive, reverse, and skip table access
Application Examples:
Suppose you now have a Student object collection class, and the client does not want to care about its internal details, it can only traverse the student object (or some of the properties of the student object). Use the iterator pattern to implement this requirement.
1. Define an iterator and an abstract interface for the collection.
interface Iterator<T> { T next(); boolean hasNext();}interface Collection<T> { boolean add(T element); boolean remove(T element);}
2. Define student object classes, for brevity, do not define too many complex attributes
Public classStudent {PrivateString ID;PrivateString name; Public Student(string ID, string name) { This. id = ID; This. name = name; } Public void SetName(String name) { This. name = name; } PublicStringgetId() {returnId } Public void setId(String ID) { This. id = ID; } PublicStringGetName() {returnName }}
3, the implementation of specific student object aggregation class, and in the internal dependency definition specific iterators.
Public class studentlist implements Collection<Student> {List<student> elementlist =NewArraylist<> (); @Override Public voidAdd (Student Element) {Elementlist.add (element); } @Override Public BooleanRemove (Student Element) {returnElementlist.remove (Element); } PublicIterator<student> Createiterator () {return NewStudentiterator (elementlist); }//Specific iterator class Private class studentiterator implements Iterator<Student> { PrivateList<student> students;Private intPosition PublicStudentiterator (list<student> students) { This. Students = students; This. Position =0; } @Override PublicStudent Next () {if(Hasnext ()) {returnStudents.get (position++); }return NULL; } @Override Public BooleanHasnext () {returnPosition < students.size (); } }}
4, use the client call, here mock a test data, convenient to call.
Public classClient { Public Static void Main(string[] args) {Studentlist studentlist =NewStudentlist (); Studentlist.add (NewStudent ("001","Zhang San")); Studentlist.add (NewStudent ("002","John Doe")); Studentlist.add (NewStudent ("004","Zhao Wu")); Studentlist.add (NewStudent ("004","Money Six")); iterator<student> Iterator = Studentlist.createiterator (); while(Iterator.hasnext ()) {Student Student = Iterator.next (); System. out. println (Student.getid () + student.getname ()); } }}
Note: Abstract iterators, abstract collection interfaces here all only declare the simplest and most commonly used methods, when development can add a variety of methods according to demand, such as java.util inside the iterator definition interface as follows:
Iterators can choose different traversal methods according to the specific aggregation: positive sequence, reverse order, skip table, etc. Instead of traversing the entire object, perhaps you point to a property that traverses the object, as in the example above you point to traverse the student's number, or the number is used most diligently, then you define an iterative method NextID to traverse the student's school number.
On design pattern: iterator mode (Iterator pattern)