Author: A thousand
Email address: [email protected]
Official account: Thousand jue
Title: Iterator Patterns for Design patterns
Hello, ladies and gentlemen, THOUSANDS of unique I come back to update, recently happened a life event, resulting in my dove for a week, is thousands of unique look at the house for a week, finally rely on their own efforts to let my family to help me buy a house (hahaha) I believe you can also look at the gentlemen.
Good not to joke, because qianjui will get married at the end of the year, so take advantage of now to look at the wedding house, I feel my efficiency of buying a house is quite fast, I think I met my sales are happy to fly, I contacted the house sales on Thursday night, saw the house on Saturday morning, the afternoon to pay the down payment. My daughter-in-law said that the pace is too fast, I also feel that the pace is very fast, so when the leek was tied up by the house, no longer can eat what to eat, from then on the back of the mortgage.
Ok now anyway (at the end of the article have eggs), a thousands off in front of the observer pattern is introduced, don’t know how you see the officer, do you have any understanding of the observer pattern is a bit new, and if so then thousands of unique to reach the purpose of this article qian will introduce the iterator pattern of design patterns, Although we will probably not write this mode by ourselves now, because Java has been done very perfect, but there is an old saying that we can better move forward by learning history and seeing the direction clearly.
This article will use an example to explain what the iterator pattern is.
define
First of all, we usually refer to the content of Baidu Encyclopedia
The Iterator pattern provides a way to sequentially access the various elements of an aggregate object without exposing the internal representation of that object.
The iterator pattern is a method that allows sequential access to elements inside an object without revealing what data structure is used inside the object
Understand or can understand, if not understand the above words, thousands of must use the code to explain to you.
code
Defines an interface for an iterator
public interface Iterator<T> {
// Returns the first element of the object
T first(a);
// Returns the next element of the object
T next(a);
// Determine if the object has a next element
boolean hasNext(a);
// Returns the current element of the object
T getCurrentObj(a);
}
Copy the code
Define a class that implements iterators
public class ConcreateIterator<T> implements Iterator<T> {
List<T> list ;
int current =0;
public ConcreateIterator(List<T> list){
this.list = list;
}
@Override
public T first(a) {
return list.get(0);
}
@Override
public T next(a) {
T t = null;
if(this.hasNext()){
t = list.get(current);
current ++ ;
}
return t;
}
@Override
public boolean hasNext(a) {
if(current < list.size()){
return true;
}
return false;
}
@Override
public T getCurrentObj(a) {
returnlist.get(current); }}Copy the code
Define an aggregation interface
public interface Aggregate<T> {
// Add an object
void add(T t);
// Delete an object
void remove(T t);
// Generate traversal
Iterator<T> createIteratory(a);
}
Copy the code
Define a class that implements an aggregate interface
public class ConcreateAggregate<T> implements Aggregate<T> {
private List<T> list = new ArrayList();
@Override
public void add(T t) {
list.add(t);
}
@Override
public void remove(T t) {
list.remove(t);
}
@Override
public Iterator createIteratory(a) {
return newConcreateIterator(list); }}Copy the code
Define a test class
public class TestIterator {
public static void main(String []args){
ConcreateAggregate<String> concreateAggregate= new ConcreateAggregate<String>();
concreateAggregate.add("Test");
concreateAggregate.add(1 "test");
concreateAggregate.add("The test 2");
concreateAggregate.add("Test 3");
concreateAggregate.add("Test four.");
concreateAggregate.add("Test 5");
concreateAggregate.add("Test 6");
concreateAggregate.remove("Test four.");
Iterator iterator = concreateAggregate.createIteratory();
System.out.println("The first is:" + iterator.first());
System.out.println("The current element is:" + iterator.getCurrentObj());
while(iterator.hasNext()){ System.out.println(iterator.next()); }}}Copy the code
conclusion
See here believe that you always think the above code reader masters are familiar with, yes thousand absolutely is in reference to the realization of the JDK iterator pattern example, Java now already very good, on the use of the iterator we basically don’t have to write their own iterator now, may be thousands of unique now write code is too simple, not met, write their own iterator So the introduction of a little rough also please look at the gentlemen do not mind.
Next period: prototype mode.
If you like thousands of people can pay attention to, comment, forward.
Finally, you think the new house should start with what smart home can improve the quality of life.