设计模式11:迭代器模式

迭代器模式(Iterator DP)的定义是:提供一种方式来顺序地访问聚合(aggregate)里的对象而不需暴露其底层的表现(原文:Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation)。
也就是说,迭代器模式是对遍历的封装,调用该接口的方法不需要知道元素是怎么来的,也不需要这些元素在其原来的地方是以什么形式存在的。
迭代器模式有很明显的解耦意图,符合最少知识原则。
Java里面有Iterator接口。事实上,以前的是Enumeration。

代码:
对象Item以及配套的枚举:

/**
 * 
 * Item
 *
 */
public class Item {

  private ItemType type;
  private String name;

  public Item(ItemType type, String name) {
    this.setType(type);
    this.name = name;
  }

  @Override
  public String toString() {
    return name;
  }

  public ItemType getType() {
    return type;
  }

  public final void setType(ItemType type) {
    this.type = type;
  }
}
/**
 * 
 * ItemType enumeration
 *
 */
public enum ItemType {

  ANY, WEAPON, RING, POTION

}

Item迭代器接口:

/**
 * 
 * ItemIterator interface.
 * 
 */
public interface ItemIterator {

  boolean hasNext();

  Item next();
}

宝箱类,是Item的集合:

/**
 * 
 * TreasureChest, the collection class.
 * 
 */
public class TreasureChest {

  private List items;

  /**
   * Constructor
   */
  public TreasureChest() {
    items = new ArrayList<>();
    items.add(new Item(ItemType.POTION, "Potion of courage"));
    items.add(new Item(ItemType.RING, "Ring of shadows"));
    items.add(new Item(ItemType.POTION, "Potion of wisdom"));
    items.add(new Item(ItemType.POTION, "Potion of blood"));
    items.add(new Item(ItemType.WEAPON, "Sword of silver +1"));
    items.add(new Item(ItemType.POTION, "Potion of rust"));
    items.add(new Item(ItemType.POTION, "Potion of healing"));
    items.add(new Item(ItemType.RING, "Ring of armor"));
    items.add(new Item(ItemType.WEAPON, "Steel halberd"));
    items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
  }

  ItemIterator iterator(ItemType itemType) {
    return new TreasureChestItemIterator(this, itemType);
  }

  /**
   * Get all items
   */
  public List getItems() {
    List list = new ArrayList<>();
    list.addAll(items);
    return list;
  }

}

宝箱的迭代器:

/**
 * 
 * TreasureChestItemIterator
 *
 */
public class TreasureChestItemIterator implements ItemIterator {

  private TreasureChest chest;
  private int idx;
  private ItemType type;

  /**
   * Constructor
   */
  public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
    this.chest = chest;
    this.type = type;
    this.idx = -1;
  }

  @Override
  public boolean hasNext() {
    return findNextIdx() != -1;
  }

  @Override
  public Item next() {
    idx = findNextIdx();
    if (idx != -1) {
      return chest.getItems().get(idx);
    }
    return null;
  }

  private int findNextIdx() {

    List items = chest.getItems();
    boolean found = false;
    int tempIdx = idx;
    while (!found) {
      tempIdx++;
      if (tempIdx >= items.size()) {
        tempIdx = -1;
        break;
      }
      if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
        break;
      }
    }
    return tempIdx;
  }
}

测试:

  public static void main(String[] args) {
    TreasureChest chest = new TreasureChest();

    ItemIterator ringIterator = chest.iterator(ItemType.RING);
    while (ringIterator.hasNext()) {
      LOGGER.info(ringIterator.next().toString());
    }

    LOGGER.info("----------");

    ItemIterator potionIterator = chest.iterator(ItemType.POTION);
    while (potionIterator.hasNext()) {
      LOGGER.info(potionIterator.next().toString());
    }

    LOGGER.info("----------");

    ItemIterator weaponIterator = chest.iterator(ItemType.WEAPON);
    while (weaponIterator.hasNext()) {
      LOGGER.info(weaponIterator.next().toString());
    }

    LOGGER.info("----------");

    ItemIterator it = chest.iterator(ItemType.ANY);
    while (it.hasNext()) {
      LOGGER.info(it.next().toString());
    }
  }
}

TreasureChest有给出一个ItemType来获得对应的宝箱迭代器的函数,外部可以通过这个函数来得到迭代器然后进行迭代。

你可能感兴趣的:(设计模式11:迭代器模式)