File tree Expand file tree Collapse file tree
src/designpattern/iterator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package designpattern .iterator ;
2+
3+ /**
4+ * 聚集接口
5+ *
6+ * @author liu yuning
7+ *
8+ * @param <T>
9+ */
10+ public interface Aggregate <T > {
11+
12+ public Iterator <T > createIterator ();
13+ }
Original file line number Diff line number Diff line change 1+ package designpattern .iterator ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ /**
7+ * 具体聚集类
8+ *
9+ * @author liu yuning
10+ *
11+ * @param <T>
12+ */
13+ public class ConcreteAggregate <T > implements Aggregate <T > {
14+
15+ private List <T > items = new ArrayList <T >();
16+
17+ @ Override
18+ public Iterator <T > createIterator () {
19+ // TODO Auto-generated method stub
20+ return new ConcreteIterator <T >(this );
21+ }
22+
23+ public int count () {
24+ return items .size ();
25+ }
26+
27+ public T getItems (int index ) {
28+ return items .get (index );
29+ }
30+
31+ public void setItems (T item ) {
32+ items .add (item );
33+ }
34+
35+ }
Original file line number Diff line number Diff line change 1+ package designpattern .iterator ;
2+
3+ /**
4+ * 具体迭代器类,给出一种具体迭代的实现方式。思考:迭代器表示的是一种迭代的行为,而聚集则是真正要被迭代的数据集合。
5+ * 之所以要将迭代器和聚集分开,就是为了将行为与数据分开。 可类比Java中Iterator与Iterable的关系进行理解
6+ *
7+ * @author liu yuning
8+ *
9+ * @param <T>
10+ */
11+ public class ConcreteIterator <T > implements Iterator <T > {
12+
13+ private ConcreteAggregate <T > concreteAggregate ;
14+ private int current = 0 ;
15+
16+ public ConcreteIterator (ConcreteAggregate <T > concreteAggregate ) {
17+ this .setConcreteAggregate (concreteAggregate );
18+ }
19+
20+ @ Override
21+ public T first () {
22+ return concreteAggregate .getItems (0 );
23+ }
24+
25+ @ Override
26+ public T next () {
27+ current ++;
28+
29+ if (current < concreteAggregate .count ()) {
30+ return concreteAggregate .getItems (current );
31+ }
32+
33+ return null ;
34+ }
35+
36+ @ Override
37+ public boolean isDone () {
38+ return current >= concreteAggregate .count () ? true : false ;
39+ }
40+
41+ @ Override
42+ public T currentItem () {
43+ return concreteAggregate .getItems (current );
44+ }
45+
46+ public ConcreteAggregate <T > getConcreteAggregate () {
47+ return concreteAggregate ;
48+ }
49+
50+ public void setConcreteAggregate (ConcreteAggregate <T > concreteAggregate ) {
51+ this .concreteAggregate = concreteAggregate ;
52+ }
53+
54+ public int getCurrent () {
55+ return current ;
56+ }
57+
58+ public void setCurrent (int current ) {
59+ this .current = current ;
60+ }
61+
62+ }
Original file line number Diff line number Diff line change 1+ package designpattern .iterator ;
2+
3+ /**
4+ * 迭代器接口
5+ *
6+ * @author liu yuning
7+ *
8+ * @param <T>
9+ */
10+ public interface Iterator <T > {
11+
12+ public T first ();
13+
14+ public T next ();
15+
16+ public boolean isDone ();
17+
18+ public T currentItem ();
19+
20+ }
Original file line number Diff line number Diff line change 1+ package designpattern .iterator ;
2+
3+ /**
4+ * 迭代器客户端
5+ *
6+ * @author liu yuning
7+ *
8+ */
9+ public class IteratorClient {
10+ public static void main (String [] args ) {
11+ ConcreteAggregate <String > bus = new ConcreteAggregate <String >();
12+
13+ bus .setItems ("大鸟" );
14+ bus .setItems ("小菜" );
15+ bus .setItems ("行李" );
16+ bus .setItems ("老外" );
17+ bus .setItems ("公交内部员工" );
18+ bus .setItems ("小偷" );
19+
20+ Iterator <String > iterator = new ConcreteIterator <String >(bus );
21+
22+ while (!iterator .isDone ()) {
23+ System .out .println (iterator .currentItem () + "请买票!" );
24+ iterator .next ();
25+ }
26+ }
27+
28+ }
You can’t perform that action at this time.
0 commit comments