java设计模式进阶_iterator

原创 2016年08月30日 10:07:04

这里写图片描述

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






版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

java设计模式进阶_composite

import java.util.ArrayList; import java.util.List;// // // Generated by StarUML(tm) Java Add-In // ...

java进阶之--java设计模式

  • 2011-07-28 10:54
  • 322KB
  • 下载

java设计模式进阶_event-aggregator

package com.think.in.java.design.pattern.lesson27;// // // Generated by StarUML(tm) Java Add-In // ...

java设计模式进阶_prototype

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : HeroFac...

java设计模式进阶_state

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : State.j...

java设计模式进阶_servant

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : Royalty...

java设计模式进阶_model-view-presenter

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : FileSel...

java设计模式进阶_template-method

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : Stealin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)