北辰

c/c++/java程序

java设计模式进阶_iterator

这里写图片描述

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ItemIterator.java
//  @ Date : 2016/8/30
//  @ Author : 
//
//




public interface ItemIterator {
    public boolean hasNext();
    public Item next();
}

import java.util.List;

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : TreasureChestltemIterator.java
//  @ Date : 2016/8/30
//  @ Author : 
//
//




public class TreasureChestItemIterator implements ItemIterator {
    private int idx;
    private TreasureChest chest;
    private ItemType type;

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

    private int findNextIdx() {
        List<Item> 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 boolean hasNext() {
        return findNextIdx() != -1;
    }

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


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Item.java
//  @ Date : 2016/8/30
//  @ Author : 
//
//




public class Item {
    private String name;
    private ItemType type;

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

    public String toString() {
        return name;
    }

    public ItemType getType() {
        return type;
    }

    public void setType(ItemType type) {
        this.type = type;
    }
}


import java.util.ArrayList;
import java.util.List;

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : TreasureChest.java
//  @ Date : 2016/8/30
//  @ Author : 
//
//




public class TreasureChest {

    private List<Item> items;

    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"));
    }

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

    public List<Item> getItems() {
        ArrayList<Item> list = new ArrayList<>();
        list.addAll(items);
        return list;
    }
}


public enum ItemType {
    ANY,
    WEAPON,
    RING,
    POTION
}



import java.util.List;
           //TreasureChestItemIterator
public class TreasureChestItemIterator implements ItemIterator {

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

    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<Item> 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 class App {

    public static void main(String[] args) {

        TreasureChest chest = new TreasureChest();
        ItemIterator ringIterator = chest.iterator(ItemType.RING);
        while(ringIterator.hasNext())
        {
            System.out.println(ringIterator.next());
        }

        System.out.println("------------");

        ItemIterator potionIterator = chest.iterator(ItemType.POTION);
        while(potionIterator.hasNext())
        {
            System.out.println(potionIterator.next());;
        }

        System.out.println("------------");

        ItemIterator weaponIterator = chest.iterator(ItemType.WEAPON);
        while(weaponIterator.hasNext())
        {
            System.out.println(weaponIterator.next());
        }

        System.out.println("------------");

        ItemIterator it = chest.iterator(ItemType.ANY);
        while(it.hasNext())
        {
            System.out.println(it.next());
        }

    }

}

/*
Ring of shadows
Ring of armor
------------
Potion of courage
Potion of wisdom
Potion of blood
Potion of rust
Potion of healing
------------
Sword of silver +1
Steel halberd
Dagger of poison
------------
Potion of courage
Ring of shadows
Potion of wisdom
Potion of blood
Sword of silver +1
Potion of rust
Potion of healing
Ring of armor
Steel halberd
Dagger of poison
*/






阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwww_com/article/details/52366895
文章标签: java设计模式
个人分类: java设计模式进阶
想对作者说点什么? 我来说一句

java设计模式

2012年05月29日 1MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭