Skip to content

Commit

Permalink
完成 迭代器模式 Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesZBL committed Nov 27, 2017
1 parent 41923ed commit a021905
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 2 deletions.
60 changes: 60 additions & 0 deletions iterator/src/main/java/Application.java
@@ -0,0 +1,60 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Iterator
*/
public class Application {

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

public static void main(String[] args) {
// 查找小说
ItemIterator iterator = new BookShelfIterator(ItemType.FICTION, new BookShelf());
LOGGER.info("正在查找小说类图书");
while (iterator.hasNext()) {
Item nextItem = iterator.next();
LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString());
}

// 查找IT
ItemIterator iterator2 = new BookShelfIterator(ItemType.IT, new BookShelf());
LOGGER.info("正在查找IT类图书");
while (iterator2.hasNext()) {
Item nextItem = iterator2.next();
LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString());
}

// 查找漫画
ItemIterator iterator3 = new BookShelfIterator(ItemType.CARTOON, new BookShelf());
LOGGER.info("正在查找漫画类图书");
while (iterator3.hasNext()) {
Item nextItem = iterator3.next();
LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString());
}
}
}
55 changes: 55 additions & 0 deletions iterator/src/main/java/BookShelf.java
@@ -0,0 +1,55 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

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

/**
* 书架
*/
public class BookShelf {

private List<Item> items;

public BookShelf() {
items = new ArrayList<>();
items.add(new Item(ItemType.FICTION, "西游记"));
items.add(new Item(ItemType.FICTION, "水浒传"));
items.add(new Item(ItemType.FICTION, "三国演义"));
items.add(new Item(ItemType.FICTION, "红楼梦"));
items.add(new Item(ItemType.CARTOON, "阿衰"));
items.add(new Item(ItemType.CARTOON, "七龙珠"));
items.add(new Item(ItemType.CARTOON, "火影忍者"));
items.add(new Item(ItemType.IT, "设计模式-可复用面向对象软件的基础"));
items.add(new Item(ItemType.IT, "重构-改善既有代码的设计"));
items.add(new Item(ItemType.IT, "Effective Java"));
items.add(new Item(ItemType.IT, "Java编程思想"));
}

public List<Item> getItemList() {
List<Item> list = new ArrayList<>();
list.addAll(items);
return list;
}
}
72 changes: 72 additions & 0 deletions iterator/src/main/java/BookShelfIterator.java
@@ -0,0 +1,72 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import java.util.List;

/**
* 图书的迭代器
*/
public class BookShelfIterator implements ItemIterator {

private ItemType type;
private BookShelf shelf;
private int idx;

public BookShelfIterator(ItemType type, BookShelf shelf) {
this.type = type;
this.shelf = shelf;
this.idx = -1;
}

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

@Override
public Item next() {
idx = getNexIdx();
if (-1 != idx) {
return shelf.getItemList().get(idx);
}
return null;
}

private int getNexIdx() {
List<Item> list = shelf.getItemList();
int tempIdx = idx;
boolean found = false;
while (!found) {
tempIdx++;
if (tempIdx >= list.size()) {
tempIdx = -1;
break;
}
if (list.get(tempIdx).getType().equals(type)) {
break;
}
}
return tempIdx;
}
}
2 changes: 1 addition & 1 deletion iterator/src/main/java/Item.java
Expand Up @@ -45,6 +45,6 @@ public ItemType getType() {

@Override
public String toString() {
return super.toString();
return name;
}
}
33 changes: 33 additions & 0 deletions iterator/src/main/java/ItemIterator.java
@@ -0,0 +1,33 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* 迭代器接口
*/
public interface ItemIterator {

boolean hasNext();

Item next();
}
2 changes: 1 addition & 1 deletion iterator/src/main/java/ItemType.java
Expand Up @@ -27,5 +27,5 @@
*/
public enum ItemType {

ANY, VEHICLE, ANIMAL, FOOD;
IT, FICTION, CARTOON
}

0 comments on commit a021905

Please sign in to comment.