Java设计模式--迭代器模式(Iterator)

概述


  • 在软件开发中,我们经常需要使用聚合对象来存储一系列数据。聚合对象拥有两个职责:一是存储数据;二是遍历数据。从依赖性来看,前者是聚合对象的基本职责;而后者既是可变化的,又是可分离的。因此,可以将遍历数据的行为从聚合对象中分离出来,封装在一个被称之为“迭代器”的对象中,由迭代器来提供遍历聚合对象内部数据的行为,这将简化聚合对象的设计,更符合“单一职责原则”的要求。
  • 定义:提供一种方法访问一个容器(container)对象中各元素,而又不需暴露该对象的内部细节。
  • 又叫做游标(Cursor)模式。
  • 由客户程序来控制遍历的进程,被称为外部迭代器;还有一种实现方式便是由迭代器自身来控制迭代,被称为内部迭代器。
  • 迭代器模式是一种对象行为型模式。
  • 学习难度:★★★☆☆
  • 使用频率:★★★★★

优缺点


  • 优点
  • 缺点

类图


在这里插入图片描述

组成角色


  • 迭代器角色(Iterator)
  • 具体迭代器角色(ConcreteIterator)
  • 容器角色(Container)
  • 具体容器角色(ConcreteContainer)

Code Example


迭代器角色(Iterator)
  • hasNext
  • next
/**
 * 迭代器角色(Iterator):迭代器角色负责定义访问和遍历元素的接口。
 * 
 * @author yanbin
 * 
 */
public interface Iterator {

   public boolean hasNext();

   public Object next();

}
具体迭代器角色(ConcreteIterator)
/**
 * 具体迭代器角色(Concrete Iterator):具体迭代器角色要实现迭代器接口,并要记录 遍历中的当前位置。
 * 
 * @author yanbin
 * 
 */
public class BallIterator implements Iterator {

   // 指明操作谁
   private BallContainer ballContainer;
   private int index;

   public BallIterator(BallContainer ballContainer) {
      // 将操作的“盆”传入,参考上面的代码
      this.ballContainer = ballContainer;
      this.index = 0;
   }

   // 实现迭代器中所具有的方法
   public boolean hasNext() {
      if (index < ballContainer.getCount()) {
         return true;
      } else {
         return false;
      }
   }

   public Object next() {
      Ball ball = ballContainer.getBallAt(index);
      index++;
      return ball;
   }

}
容器角色(Container)
/**
 * 容器角色(Container):容器角色负责提供创建具体迭代器角色的接口。
 * 
 * @author yanbin
 * 
 */
public interface Container {

   public Iterator iterator();

}
具体容器角色(ConcreteContainer)
/**
 * 集合中的元素,pojo
 * 
 * @author yanbin
 * 
 */
public class Ball {

   private String ballColor;

   public Ball(String ballColor) {
      this.ballColor = ballColor;
   }

   public String getBallColor() {
      return this.ballColor;
   }

}
/**
 * 具体容器角色(Concrete Container):具体容器角色实现创建具体迭代器角色的接口 ——这个具体迭代器角色于该容器的结构相关。
 * 
 * @author yanbin
 * 
 */
public class BallContainer implements Container {

   private ArrayList<Ball> balls;

   public BallContainer() {
      balls = new ArrayList<Ball>();
   }

   public Ball getBallAt(int index) {
      return balls.get(index);
   }

   public void appendBall(Ball ball) {
      balls.add(ball);
   }

   public int getCount() {
      return balls.size();
   }

   public ArrayList<Ball> getBalls() {
      return this.balls;
   }

   public Iterator iterator() {
      return new BallIterator(this);
   }

}
客户端
/**
 * 迭代器模式:又叫做游标(Cursor)模式。提供一种 方法访问一个容器(container)对象中各元素,而又不需暴露该对象的内部细节。
 * 组成:迭代器角色(Iterator);具体迭代器角色(Concrete Iterator);容器角色(Container);具体容器角色(Concrete
 * Container); 迭代器模式在客户与容器之间加入了迭代器角色。<br>
 * 迭代器模式抽象了具体迭代器角色, 使得客户程序更具一般性和重用性。这被称为多态迭代。<br>
 * 是由客户程序来控制遍历的进程,被称为外部迭代器;还有一种实现方式便是由迭代器自身来控制迭代,被称为内部迭代器。
 * 
 * @author yanbin
 * 
 */
public class IteratorPattern {
   
   public static void main(String args[]) {
      BallContainer ballContainer = new BallContainer();
      ballContainer.appendBall(new Ball("red"));
      ballContainer.appendBall(new Ball("blue"));
      ballContainer.appendBall(new Ball("yellow"));
      Iterator ite = ballContainer.iterator();
      while (ite.hasNext()) {
         Ball ball = (Ball) ite.next();
         System.out.println(ball.getBallColor());
      }
   }
   
}

猜你喜欢

转载自blog.csdn.net/yanbin0830/article/details/88884033