HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Iterative mode (Iterator Pattern) Java and .Net programming environment is a very common design pattern. This mode is used for sequential access elements of a collection of objects, a collection of objects do not need to know the underlying representation.
Iterator pattern belongs behavioral patterns.
Intent: Provide a way to access an object in each element of the polymerization, but no need to expose the interior of the object.
Mainly to solve: a different way to traverse the entire integration object.
When to use: traversing a polymeric object.
How to fix: the walk between the elements of the responsibility to iterators instead of aggregate objects.
The key code: define the interface: hasNext, next.
Application examples: JAVA in the iterator.
Advantages: 1, which supports a different way to traverse an aggregate object.2, simplifying the polymerization iterator class. 3, in the same polymerization can have multiple traversal. 4, in an iterative mode, add a new class of polymeric iterator class and easy, without modifying the original code.
Disadvantages: Due to the separation of duties iterator pattern data storage and traverse data, add new aggregated classes require a corresponding increase in new iterator class, the number of Class pairs increases, which increases the complexity of the system to some extent.
Usage scenarios: 1, access to a polymeric content object without having to expose its internal representation.2, the need to provide a variety of aggregate object traversal method. 3 for traversing different aggregate structure provides a unified interface.
Notes: iterative mode is separated traversal behavior of a collection of objects, abstract an iterator class to be responsible, so that both can be done without exposure to the internal structure of the collection, but also let external code transparent access to internal data collection.
We will create a narrativeIteratorinterface and a navigation method Returns an iterator of theContainerinterface. Entity classContainerimplements the interface will be responsible for implementingthe Iteratorinterface.
IteratorPatternDemo,our demonstration classes use the entity class to printNamesRepositoryNamesRepository stored asNamescollection.
Create an interface.
Iterator.java
public interface Iterator { public boolean hasNext (); public Object next (); }
Container.java
public interface Container { public Iterator getIterator (); }
Create entity classContainerimplements the interface. This class has realized the internal classNameIteratorIterator interface.
NameRepository.java
public class NameRepository implements Container { public String names [] = { "Robert", "John", "Julie", "Lora"}; @Override public Iterator getIterator () { return new NameIterator (); } private class NameIterator implements Iterator { int index; @Override public boolean hasNext () { if (index <names.length) { return true; } return false; } @Override public Object next () { if (this.hasNext ()) { return names [index ++]; } return null; } } }
UseNameRepositoryto get iterators, and print the names.
IteratorPatternDemo.java
public class IteratorPatternDemo { public static void main (String [] args) { NameRepository namesRepository = new NameRepository (); for (Iterator iter = namesRepository.getIterator (); iter.hasNext ();) { String name = (String) iter.next (); System.out.println ( "Name:" + name); } } }
Verify output.
Name: Robert Name: John Name: Julie Name: Lora