Design pattern of Zen design pattern-iterator mode

Source: Internet
Author: User

One: the definition of the iterator pattern
---> Iterator mode (Iterator pattern) is now a declining pattern, and basically no one will write an iterator alone, unless it is a product-nature development
---> It provides a way to access individual elements in a container object without exposing the object's internal details.
---> iterators are services for containers, what is a container? All types that can hold objects can be called containers, such as collection collection types, set types, and so on, and iterator patterns are created to solve the elements that traverse these containers.
The---> Iterator mode provides the convenience of traversing the container as long as it manages the addition or subtraction of the elements, and it needs to be iterated over.



Second: The role of the iterator pattern
Iterator Abstract iterator
Abstract iterators are responsible for defining the interfaces that access and traverse elements, and basically there are 3 fixed methods: First () gets the second element, next () accesses the next element, IsDone () has access to the bottom (Java is called the Hasnext () method)
Concreteiterator specific iterators
The specific iterator role implements the iterator interface to complete the traversal of the container element.
Aggregate abstract Container
The container role is responsible for providing an interface that creates a specific iterator role, and inevitably provides a method like Createiterator (), which is generally the iterator () method in Java.
Concrete Aggregate Concrete Container
The specific container implements the method of the container interface definition, creating an object that accommodates the iterator.


Three: an example of an iterator pattern
"1" Iterator abstract class

1  PackageCom.yeepay.sxf.template15;2 /**3 * Abstract class of iterators4  * @authorSXF5  *6  * @param<E>7  */8  Public InterfaceIterator<e> {9     //traverse the next elementTen      PublicE Next (); One     //do you have the next element ? A      Public BooleanHasnext (); -     //Delete the currently pointing element -      Public Booleanremove (); the}
View Code

"2" Iterator implementation class

1  PackageCom.yeepay.sxf.template15;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 /**6 * Self-implemented iterators7  * @authorSXF8  *9  * @param<E>Ten  */ One  Public classMyitrator<e>ImplementsIterator<e> { A  -     PrivateList<e> list=NewArraylist<e>(); -      the     Private intCursor=0; -      -      PublicMyitrator (list<e>e) { -          This. list=e; +     } -      + @Override A      PublicE Next () { atE e=NULL; -         if( This. Hasnext ()) { -E= This. List.get ( This. cursor++); -}Else{ -E=NULL; -         } in         returne; -     } to  + @Override -      Public BooleanHasnext () { the         if( This. cursor== This. List.size ()) { *             return false; $}Else{Panax Notoginseng             return true; -         } the     } +  A     /** the * When developing the system, the iterator's deletion method should complete two logic: one is to delete the current element, and the other is to point to the next element in the current cursor +      */ - @Override $      Public BooleanRemove () { $ list.remove (cursor); -cursor++; -         return true; the     } - Wuyi      the}
View Code

"3" Custom Collection interface

1  PackageCom.yeepay.sxf.template15;2 /**3 * Collection Interface4  * @authorSXF5  *6  * @param<E>7  */8  Public InterfaceAggregate<e> {9      Public voidAdd (E object);Ten      Public voidRemove (E object); One      PublicIterator Iterator (); A}
View Code

"4" Custom collection implementation

1  PackageCom.yeepay.sxf.template15;2 /**3 * Custom Collections4  */5 Importjava.util.ArrayList;6 Importjava.util.List;7 8  Public classMyconnection<e>ImplementsAggregate<e>{9     PrivateList<e> a=NewArraylist<e>();Ten      One @Override A      Public voidAdd (E object) { - A.add (object); -     } the  - @Override -      Public voidRemove (E object) { - A.remove (object); +     } -  + @Override A      PublicIterator Iterator () { at          -         return NewMyitrator<e> ( This. a); -     } -  -      -}
View Code

"5" Client implementation

1  PackageCom.yeepay.sxf.template15;2 3 4  Public classClienttest {5 6     7      Public Static voidMain (string[] args) {8Myconnection<string> st=NewMyconnection<string>();9St.add ("AAA");TenSt.add ("BBB"); OneSt.add ("CCC"); ASt.add ("ddd"); -Iterator iterator=st.iterator (); -          while(Iterator.hasnext ()) { theString astring=(String) Iterator.next (); -System.out.println ("Clienttest.main ()" +astring); -              -         } +          -     } +}
View Code

Design pattern of Zen design pattern-iterator mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.