码迷,mamicode.com
首页 > 编程语言 > 详细

java设计模式--行为型模式--迭代模式

时间:2014-08-01 19:05:42      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   使用   for   ar   div   

 1                                             迭代器模式
 2  概述
 3     给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
 4  
 5  
 6  适用性
 7     1.访问一个聚合对象的内容而无需暴露它的内部表示。
 8 
 9     2.支持对聚合对象的多种遍历。
10 
11     3.为遍历不同的聚合结构提供一个统一的接口(即,支持多态迭代)。
12  
13  
14  参与者
15     1.Iterator
16       迭代器定义访问和遍历元素的接口。
17 
18     2.ConcreteIterator
19       具体迭代器实现迭代器接口。
20       对该聚合遍历时跟踪当前位置。
21 
22     3.Aggregate
23       聚合定义创建相应迭代器对象的接口。
24 
25     4.ConcreteAggregate
26       具体聚合实现创建相应迭代器的接口,该操作返回ConcreteIterator的一个适当的实例.

当我看到这个模式的时候,我真的惊呆了,我联想起平时我们的for循环与迭代器。

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         List list = new ListImpl();
 5         list.add("a");
 6         list.add("b");
 7         list.add("c");
 8         //第一种迭代方式
 9         Iterator it = list.iterator();
10         while (it.hasNext()) {
11             System.out.println(it.next());
12         }
13         
14         System.out.println("=====");
15         //第二种迭代方式
16         for (int i = 0; i < list.getSize(); i++) {
17             System.out.println(list.get(i));
18         }
19     }
20 }
 1 public interface List {
 2 
 3     Iterator iterator();
 4     
 5     Object get(int index);
 6     
 7     int getSize();
 8     
 9     void add(Object obj);
10 }
 1 public class ListImpl implements List {
 2 
 3     private Object[] list;
 4     
 5     private int index;
 6     
 7     private int size;
 8     
 9     public ListImpl() {
10         index = 0;
11         size = 0;
12         list = new Object[100];
13     }
14     
15     public Iterator iterator() {
16         return new IteratorImpl(this);
17     }
18     
19     public Object get(int index) {
20         return list[index];
21     }
22     
23     public int getSize() {
24         return this.size;
25     }
26     
27     public void add(Object obj) {
28         list[index++] = obj;
29         size++;
30     }
31 }
 1 public interface Iterator {
 2 
 3     Object next();
 4     
 5     void first();
 6     
 7     void last();
 8     
 9     boolean hasNext();
10 }
 1 public class IteratorImpl implements Iterator {
 2 
 3     private List list;
 4     
 5     private int index;
 6     
 7     public IteratorImpl(List list) {
 8         index = 0;
 9         this.list = list;
10     }
11     
12     public void first() {
13         index = 0;
14     }
15 
16     public void last() {
17         index = list.getSize();
18     }
19 
20     public Object next() {
21         Object obj = list.get(index);
22         index++;
23         return obj;
24     }
25 
26     public boolean hasNext() {
27         return index < list.getSize();
28     }
29 }

 

java设计模式--行为型模式--迭代模式,布布扣,bubuko.com

java设计模式--行为型模式--迭代模式

标签:style   blog   color   java   使用   for   ar   div   

原文地址:http://www.cnblogs.com/huzi007/p/3885370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!