提供一个对象来顺序访问聚合对象中的一系列数据,而不暴露聚合对象的内部表示
既然我们要实现迭代器,那首先我们需要一个容器,迭代器就是要遍历这个容器里的数据元素;而这个容器里边存放的就是我们的业务实体类对象,容器中可以新增和删除业务实体类对象;
Iterator
- package com.behaviour.Iterator.Iterator;
-
- public interface Iterator<E> {
- boolean hasNext();
- E next();
- }
IteratorImpl
- package com.behaviour.Iterator.Concretelterator;
-
- import com.behaviour.Iterator.Iterator.Iterator;
- import java.util.List;
-
- public class IteratorImpl<E> implements Iterator<E> {
-
- private List<E> list;
- private Integer position;
-
- public IteratorImpl(List<E> list){
- this.list = list;
- position = 0;
- }
-
- @Override
- public boolean hasNext() {
- return this.position < this.list.size();
- }
-
- @Override
- public E next() {
- return this.list.get(this.position++);
- }
- }
Aggregate
- package com.behaviour.Iterator.Aggregate;
-
- import com.behaviour.Iterator.Client.ToyDomain;
- import com.behaviour.Iterator.Iterator.Iterator;
-
- public interface Aggregate {
-
- public abstract Iterator<ToyDomain> iterator();
- public abstract void add(ToyDomain toy);
-
- public abstract void remove(ToyDomain toy);
- }
AggregateImpl
- package com.behaviour.Iterator.ConcreteAggregate;
-
- import com.behaviour.Iterator.Aggregate.Aggregate;
- import com.behaviour.Iterator.Client.ToyDomain;
- import com.behaviour.Iterator.Concretelterator.IteratorImpl;
- import com.behaviour.Iterator.Iterator.Iterator;
- import java.util.ArrayList;
- import java.util.List;
-
- public class AggregateImpl implements Aggregate {
-
- private List<ToyDomain> toys;
-
- public AggregateImpl(){
- this.toys = new ArrayList<>();
- }
-
- @Override
- public Iterator<ToyDomain> iterator() {
- return new IteratorImpl<ToyDomain>(this.toys);
- }
-
- @Override
- public void add(ToyDomain toy) {
- this.toys.add(toy);
- }
-
- @Override
- public void remove(ToyDomain toy) {
- this.toys.remove(toy);
- }
- }
ToyDomain
- package com.behaviour.Iterator.Client;
-
- public class ToyDomain {
-
- private String name;
- private Double price;
-
- public ToyDomain(String name, Double price) {
- this.name = name;
- this.price = price;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Double getPrice() {
- return price;
- }
-
- public void setPrice(Double price) {
- this.price = price;
- }
- }
Test
- package com.behaviour.Iterator;
-
- import com.behaviour.Iterator.Client.ToyDomain;
- import com.behaviour.Iterator.ConcreteAggregate.AggregateImpl;
- import com.behaviour.Iterator.Iterator.Iterator;
-
- public class Test {
-
- public static void main(String[] args) {
-
- AggregateImpl aggregate = new AggregateImpl();
- aggregate.add(new ToyDomain("长江七号", 10.00));
- aggregate.add(new ToyDomain("天线宝宝", 20.00));
-
- ToyDomain temp = new ToyDomain("灰太狼", 30.00);
- aggregate.add(temp);
- aggregate.remove(temp);
-
- Iterator<ToyDomain> iterator = aggregate.iterator();
- while(iterator.hasNext()){
- ToyDomain toy = iterator.next();
- System.out.println(toy.getName() + "价值 " + toy.getPrice());
- }
- }
- }