迭代子模式

迭代子模式UML类图

迭代子模式UML类图

迭代子模式java实现

/**
* ItemType enumeration.
*/
public enum ItemType {
//任何,武器,戒指,药水
ANY, WEAPON, RING, POTION

}

/**
* Iterator interface to be implemented by iterators over various data structures.
迭代器接口,由迭代器在各种数据结构上实现。
*
* @param <T> generically typed for various objects
*/
public interface Iterator<T> {

boolean hasNext();

T next();
}

/**
* 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;
}
}

/**
* TreasureChest, the collection class.
百宝箱,集合类。
*/
public class TreasureChest {

private List<Item> items;

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

public Iterator<Item> iterator(ItemType itemType) {
return new TreasureChestItemIterator(this, itemType);
}

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

}

/**
* TreasureChestItemIterator.
百宝箱物品迭代器
*/
public class TreasureChestItemIterator implements Iterator<Item> {

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() {
var items = chest.getItems();
var tempIdx = idx;
while (true) {
tempIdx++;
if (tempIdx >= items.size()) {
tempIdx = -1;
break;
}
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
break;
}
}
return tempIdx;
}
}

/**
* The Iterator pattern is a design pattern in which an iterator is used to traverse a container and
* access the container's elements. The Iterator pattern decouples algorithms from containers.
*
* <p>In this example the Iterator ({@link Iterator}) adds abstraction layer on top of a collection
* ({@link TreasureChest}). This way the collection can change its internal implementation without
* affecting its clients.
迭代器模式是一种设计模式,其中迭代器用于遍历容器并访问容器的元素。 迭代器模式将算法与容器解耦。
<p>在此示例中,迭代器({@link Iterator})在集合({@link TreasureChest})的顶部添加了抽象层。 这样,集合可以更改其内部实现,而不会影响其客户端。
*/
public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

private static final TreasureChest TREASURE_CHEST = new TreasureChest();

private static void demonstrateTreasureChestIteratorForType(ItemType itemType) {
LOGGER.info("------------------------");
LOGGER.info("Item Iterator for ItemType " + itemType + ": ");
var itemIterator = TREASURE_CHEST.iterator(itemType);
while (itemIterator.hasNext()) {
LOGGER.info(itemIterator.next().toString());
}
}

private static void demonstrateBstIterator() {
LOGGER.info("------------------------");
LOGGER.info("BST Iterator: ");
var root = buildIntegerBst();
var bstIterator = new BstIterator<Integer>(root);
while (bstIterator.hasNext()) {
LOGGER.info("Next node: " + bstIterator.next().getVal());
}
}

private static TreeNode<Integer> buildIntegerBst() {
var root = new TreeNode<>(8);

root.insert(3);
root.insert(10);
root.insert(1);
root.insert(6);
root.insert(14);
root.insert(4);
root.insert(7);
root.insert(13);

return root;
}

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
demonstrateTreasureChestIteratorForType(RING);
demonstrateTreasureChestIteratorForType(POTION);
demonstrateTreasureChestIteratorForType(WEAPON);
demonstrateTreasureChestIteratorForType(ANY);

demonstrateBstIterator();
}
}