版权声明:欢迎转载并请注明出处,谢谢~~ https://blog.csdn.net/chimomo/article/details/81086321
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net
Definition
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Participants
The classes and/or objects participating in this pattern are:
- Iterator (AbstractIterator)
- Defines an interface for accessing and traversing elements
- ConcreteIterator (Iterator)
- Implements the Iterator interface
- Keeps track of the current position in the traversal of the aggregate
- Aggregate (AbstractCollection)
- Defines an interface for creating an Iterator object
- ConcreteAggregate (Collection)
- Implements the Iterator creation interface to return an instance of the proper ConcreteIterator
Sample Code in Java
This structural code demonstrates the Iterator pattern which provides for a way to traverse (iterate) over a collection of items without detailing the underlying structure of the collection.
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.sample;
/**
* The 'Aggregate' abstract class.
*
* @author Chimomo
*/
abstract class Aggregate {
/**
* Create iterator.
*
* @return
*/
public abstract Iterator createIterator();
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.sample;
import java.util.ArrayList;
/**
* The 'ConcreteAggregate' class.
*
* @author Chimomo
*/
class ConcreteAggregate extends Aggregate {
// The items.
private ArrayList items = new ArrayList();
/**
* Get count.
*
* @return
*/
public int getCount() {
return this.items.size();
}
/**
* Get item.
*
* @param index
* @return
*/
public Object getItem(int index) {
return this.items.get(index);
}
/**
* Set item.
*
* @param index
* @param value
*/
public void setItem(int index, Object value) {
this.items.add(index, value);
}
/**
* Create iterator.
*
* @return
*/
@Override
public Iterator createIterator() {
return new ConcreteIterator(this);
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.sample;
/**
* The 'Iterator' abstract class.
*
* @author Chimomo
*/
class ConcreteIterator extends Iterator {
// The aggregate.
private ConcreteAggregate aggregate;
// The current.
private int current;
/**
* Initializes a new instance of the "ConcreteIterator" class.
*
* @param aggregate
*/
public ConcreteIterator(ConcreteAggregate aggregate) {
this.aggregate = aggregate;
}
/**
* Current item.
*
* @return
*/
@Override
public Object currentItem() {
return this.aggregate.getItem(this.current);
}
/**
* The first item.
*
* @return
*/
@Override
public Object first() {
return this.aggregate.getItem(0);
}
/**
* Is done.
*
* @return
*/
@Override
public boolean isDone() {
return this.current >= this.aggregate.getCount();
}
/**
* The next item.
*
* @return
*/
@Override
public Object next() {
Object o = null;
if (this.current < this.aggregate.getCount() - 1) {
o = this.aggregate.getItem(++this.current);
}
return o;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.sample;
/**
* The 'Iterator' abstract class.
*
* @author Chimomo
*/
abstract class Iterator {
/**
* Current item.
*
* @return
*/
public abstract Object currentItem();
/**
* The first item.
*
* @return
*/
public abstract Object first();
/**
* Is done.
*
* @return
*/
public abstract boolean isDone();
/**
* The next item.
*
* @return
*/
public abstract Object next();
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.sample;
/**
* Startup class for Structural Iterator Design Pattern.
*
* @author Chimomo
*/
class Program {
/**
* Entry point into console application.
*
* @param args The arguments
*/
public static void main(String[] args) {
ConcreteAggregate a = new ConcreteAggregate();
a.setItem(0, "Item A");
a.setItem(1, "Item B");
a.setItem(2, "Item C");
a.setItem(3, "Item D");
// Create Iterator and provide aggregate.
ConcreteIterator i = new ConcreteIterator(a);
System.out.println("Iterating over collection:");
Object item = i.first();
while (item != null) {
System.out.println(item);
item = i.next();
}
}
}
/*
Output:
Iterating over collection:
Item A
Item B
Item C
Item D
*/
This real-world code demonstrates the Iterator pattern which is used to iterate over a collection of items and skip a specific number of items each iteration.
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.realworld;
import java.util.ArrayList;
/**
* The 'ConcreteAggregate' class.
*
* @author Chimomo
*/
class Collection implements IAbstractCollection {
// The items.
private ArrayList items = new ArrayList();
/**
* Get count.
*
* @return
*/
public int getCount() {
return this.items.size();
}
/**
* Get item.
*
* @param index
* @return
*/
public Object getItem(int index) {
return this.items.get(index);
}
/**
* Set item.
*
* @param index
* @param value
*/
public void setItem(int index, Object value) {
this.items.add(index, value);
}
/**
* Create iterator.
*
* @return
*/
@Override
public Iterator createIterator() {
return new Iterator(this);
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.realworld;
/**
* The 'Aggregate' interface.
*
* @author Chimomo
*/
interface IAbstractCollection {
/**
* Create iterator.
*
* @return
*/
Iterator createIterator();
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.realworld;
/**
* The 'Iterator' interface.
*
* @author Chimomo
*/
interface IAbstractIterator {
/**
* Current item.
*
* @return
*/
Item currentItem();
/**
* The first item.
*
* @return
*/
Item first();
/**
* Is done.
*
* @return
*/
boolean isDone();
/**
* The next item.
*
* @return
*/
Item next();
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.realworld;
/**
* A collection item.
*
* @author Chimomo
*/
class Item {
// The name.
private String name;
/**
* Initializes a new instance of the "Item" class.
*
* @param name
*/
public Item(String name) {
this.name = name;
}
/**
* Get name.
*
* @return
*/
public String getName() {
return this.name;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.realworld;
/**
* The 'ConcreteIterator' class.
*
* @author Chimomo
*/
class Iterator implements IAbstractIterator {
// The collection.
private Collection collection;
// The current.
private int current;
// The step.
private int step = 1;
/**
* Initializes a new instance of the "Iterator" class.
*
* @param collection
*/
public Iterator(Collection collection) {
this.collection = collection;
}
/**
* Set step.
*
* @param step
*/
public void setStep(int step) {
this.step = step;
}
/**
* Get the current item.
*
* @return
*/
@Override
public Item currentItem() {
return (Item) this.collection.getItem(this.current);
}
/**
* The first item.
*
* @return
*/
@Override
public Item first() {
this.current = 0;
return (Item) this.collection.getItem(this.current);
}
/**
* Is done.
*
* @return
*/
@Override
public boolean isDone() {
return this.current >= this.collection.getCount();
}
/**
* The next item.
*
* @return
*/
@Override
public Item next() {
this.current += this.step;
if (!this.isDone()) {
return (Item) this.collection.getItem(this.current);
}
return null;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.iterator.realworld;
/**
* Startup class for Real-World Iterator Design Pattern.
*
* @author Chimomo
*/
class Program {
/**
* Entry point into console application.
*
* @param args The arguments
*/
public static void main(String[] args) {
// Build a collection.
Collection collection = new Collection();
collection.setItem(0, new Item("Item 0"));
collection.setItem(1, new Item("Item 1"));
collection.setItem(2, new Item("Item 2"));
collection.setItem(3, new Item("Item 3"));
collection.setItem(4, new Item("Item 4"));
collection.setItem(5, new Item("Item 5"));
collection.setItem(6, new Item("Item 6"));
collection.setItem(7, new Item("Item 7"));
collection.setItem(8, new Item("Item 8"));
// Create iterator.
Iterator iterator = new Iterator(collection);
iterator.setStep(2);
// Skip every other item.
System.out.println("Iterating over collection:");
for (Item item = iterator.first(); !iterator.isDone(); item = iterator.next()) {
System.out.println(item.getName());
}
}
}
/*
Output:
Iterating over collection:
Item 0
Item 2
Item 4
Item 6
Item 8
*/