Java使用設計模式中迭代器模式構(gòu)建項目的代碼結(jié)構(gòu)示例
迭代器(Iterator)模式,又叫做游標(Cursor)模式。GOF給出的定義為:提供一種方法訪問一個容器(container)對象中各個元素,而又不需暴露該對象的內(nèi)部細節(jié)。
迭代器模式由以下角色組成:
迭代器角色(Iterator):迭代器角色負責定義訪問和遍歷元素的接口。
具體迭代器角色(Concrete Iterator):具體迭代器角色要實現(xiàn)迭代器接口,并要記錄遍歷中的當前位置。
容器角色(Container):容器角色負責提供創(chuàng)建具體迭代器角色的接口。
具體容器角色(Concrete Container):具體容器角色實現(xiàn)創(chuàng)建具體迭代器角色的接口。這個具體迭代器角色與該容器的結(jié)構(gòu)相關。
Java實現(xiàn)示例
類圖:
代碼:
/** * 自定義集合接口, 類似java.util.Collection * 用于數(shù)據(jù)存儲 * @author stone * */ public interface ICollection<T> { IIterator<T> iterator(); //返回迭代器 void add(T t); T get(int index); }
/** * 自定義迭代器接口 類似于java.util.Iterator * 用于遍歷集合類ICollection的數(shù)據(jù) * @author stone * */ public interface IIterator<T> { boolean hasNext(); boolean hasPrevious(); T next(); T previous(); }
/** * 集合類, 依賴于MyIterator * @author stone */ public class MyCollection<T> implements ICollection<T> { private T[] arys; private int index = -1; private int capacity = 5; public MyCollection() { this.arys = (T[]) new Object[capacity]; } @Override public IIterator<T> iterator() { return new MyIterator<T>(this); } @Override public void add(T t) { index++; if (index == capacity) { capacity *= 2; this.arys = Arrays.copyOf(arys, capacity); } this.arys[index] = t; } @Override public T get(int index) { return this.arys[index]; } }
/* * 若有新的存儲結(jié)構(gòu),可new 一個ICollection, 對應的 new 一個IIterator來實現(xiàn)它的遍歷 */ @SuppressWarnings({"rawtypes", "unchecked"}) public class Test { public static void main(String[] args) { ICollection<Integer> collection = new MyCollection<Integer>(); add(collection, 3, 5, 8, 12, 3, 3, 5); for (IIterator<Integer> iterator = collection.iterator(); iterator.hasNext();) { System.out.println(iterator.next()); } System.out.println("-------------"); ICollection collection2 = new MyCollection(); add(collection2, "a", "b", "c", 3, 8, 12, 3, 5); for (IIterator iterator = collection2.iterator(); iterator.hasNext();) { System.out.println(iterator.next()); } } static <T> void add(ICollection<T> c, T ...a) { for (T i : a) { c.add(i); } } }
打?。?/p>
3 5 8 12 3 3 5 ------------- a b c 3 8 12 3 5
相關文章
Java數(shù)據(jù)結(jié)構(gòu)之稀疏數(shù)組的實現(xiàn)與應用
這篇文章主要為大家詳細介紹了Java數(shù)據(jù)結(jié)構(gòu)中稀疏數(shù)組的實現(xiàn)與應用,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的可以了解一下2022-10-10springmvc處理模型數(shù)據(jù)Map過程解析
這篇文章主要介紹了springmvc處理模型數(shù)據(jù)Map過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01spring security 5.x實現(xiàn)兼容多種密碼的加密方式
spring security針對該功能有兩種實現(xiàn)方式,一種是簡單的使用加密來保證基于 cookie 的 token 的安全,另一種是通過數(shù)據(jù)庫或其它持久化存儲機制來保存生成的 token。這篇文章主要給大家介紹了關于spring security 5.x實現(xiàn)兼容多種密碼的加密方式,需要的朋友可以參考下。2018-01-01Mybatis-Plus同時使用邏輯刪除和唯一索引的問題及解決辦法(報數(shù)據(jù)重復Duplicate entry的
在開發(fā)中,我們經(jīng)常會有邏輯刪除和唯一索引同時使用的情況,但當使用mybatis plus時,如果同時使用邏輯刪除和唯一索引,會報數(shù)據(jù)重復Duplicate entry的問題,如何解決這個問題呢,小編給大家分享Mybatis-Plus同時使用邏輯刪除和唯一索引的問題及解決辦法,一起看看吧2023-11-11springboot application無法使用$獲取pom變量的問題及解決
這篇文章主要介紹了springboot application無法使用$獲取pom變量的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02解決springboot 2.x集成log4j2調(diào)試日志無法關閉的問題
這篇文章主要介紹了解決springboot 2.x集成log4j2調(diào)試日志無法關閉的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07