MyBatis框架迭代器模式實(shí)現(xiàn)原理解析
迭代器模式,一直沒(méi)用過(guò),也不會(huì)用。恰巧MyBatis框架中也使用到了迭代器模式,而且看起來(lái)還比較簡(jiǎn)單,在以后的工作中,若有需要咱們可模仿它的套路來(lái)干。
直接上代碼
import java.util.Iterator;/** * @author Clinton Begin */public class PropertyTokenizer implements Iterator<PropertyTokenizer> { private String name; private final String indexedName; private String index; private final String children; // 通過(guò)這個(gè)children屬性建立前后兩次迭代的關(guān)系 public PropertyTokenizer(String fullname) { int delim = fullname.indexOf(’.’); if (delim > -1) { name = fullname.substring(0, delim); children = fullname.substring(delim + 1); } else { name = fullname; children = null; } indexedName = name; delim = name.indexOf(’[’); if (delim > -1) { index = name.substring(delim + 1, name.length() - 1); name = name.substring(0, delim); } } public String getName() { return name; } public String getIndex() { return index; } public String getIndexedName() { return indexedName; } public String getChildren() { return children; } @Override public boolean hasNext() { return children != null; } @Override public PropertyTokenizer next() { return new PropertyTokenizer(children); } @Override public void remove() { throw new UnsupportedOperationException('Remove is not supported, as it has no meaning in the context of properties.'); }}
實(shí)現(xiàn) Iterator 接口就很方便的弄出一個(gè)迭代器,然后就可以使用hasNext和next方法了。
業(yè)務(wù)邏輯咱們不用管,只需要知道在調(diào)用next方法時(shí),new了一個(gè) PropertyTokenizer 實(shí)例, 而這個(gè)實(shí)例有個(gè) children屬性, hasNext方法就是通過(guò)判斷這個(gè)children屬性是否為空來(lái)作為結(jié)束迭代的判斷條件。
具體的實(shí)現(xiàn)的我們不管,只需要領(lǐng)悟兩點(diǎn): 1. next需要干啥; 2. hasNext的如何判斷?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. MySQL創(chuàng)始人發(fā)郵件尋求中國(guó)幫助2. 快速刪除ORACLE重復(fù)記錄3. MYSQL數(shù)據(jù)庫(kù)存文本轉(zhuǎn)存數(shù)據(jù)庫(kù)問(wèn)題4. ACCESS轉(zhuǎn)SQL數(shù)據(jù)庫(kù)相關(guān)的幾個(gè)技能5. 學(xué)好Oracle的六條總結(jié)6. 巧用SQL語(yǔ)言在ACCESS數(shù)據(jù)庫(kù)中批量替換內(nèi)容7. MySQL存儲(chǔ)過(guò)程例子(包含事務(wù)、參數(shù)、嵌套調(diào)用、游標(biāo)循環(huán)等)8. MySQL學(xué)習(xí)記錄之KEY分區(qū)引發(fā)的血案9. mysql like語(yǔ)句問(wèn)題10. Mysql入門(mén)系列:MYSQL列類(lèi)型選擇與MYSQL查詢效率

網(wǎng)公網(wǎng)安備