JAVA: Implementing Collection Enumeration

Traditionally, we use loops to iterate collections:

Loops Iterating List

To traverse a Collection, we need to write some kind of loops: for-each, while-loop, etc. Writing all these loops is easy, but it soon becomes tedious, slowing down our coding process; And being tedious often means being error-prone. We must develop some way to solve the problem.

Looking at these loops closely, we can easily discover that they can be divided into two parts: the code inside the loop and the loop that wraps the code inside it:


The Anatomy of A Loop

In the anatomy above, we can see that:

1) Only the code inside the loop wrapper varies;?

2) The loop, as a wrapper of the code inside it, remains the same, and thus can be factored out.

With all these observations, I employed the so called Strategy Design Pattern and devised the following code:

Enumeration v1

class StopFlag is introduced as a communication mechanism between the enumeration method of interface Enumeration and its caller so the method can tell its caller to stop when appropriate.

Working, but not so elegant, you might think. So did I. So after a second thought, I did some refinement and came up with the following code:

Enumeration v2

Here the revised version eliminates the StopFlag class, saving its job for the return value of the enumeration method.

Future Refactor

With the introduction of lambda, a new feature of JAVA SE 8, this strategy can be taken further to an extreme where the Enumeration interface and its anonymous inner class implementation can be removed. That way, the code can be even more compact.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蓄氧,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鲤氢,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機档叔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒸绩,“玉大人蹲蒲,你說我怎么就攤上這事∏止螅” “怎么了届搁?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長窍育。 經(jīng)常有香客問我卡睦,道長,這世上最難降的妖魔是什么漱抓? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任表锻,我火速辦了婚禮,結(jié)果婚禮上乞娄,老公的妹妹穿的比我還像新娘瞬逊。我一直安慰自己显歧,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布确镊。 她就那樣靜靜地躺著士骤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蕾域。 梳的紋絲不亂的頭發(fā)上拷肌,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音旨巷,去河邊找鬼巨缘。 笑死,一個胖子當(dāng)著我的面吹牛采呐,可吹牛的內(nèi)容都是我干的若锁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼斧吐,長吁一口氣:“原來是場噩夢啊……” “哼拴清!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起会通,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤口予,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后涕侈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沪停,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年裳涛,在試婚紗的時候發(fā)現(xiàn)自己被綠了木张。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡端三,死狀恐怖舷礼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情郊闯,我是刑警寧澤妻献,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站团赁,受9級特大地震影響育拨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜欢摄,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一熬丧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧怀挠,春花似錦析蝴、人聲如沸害捕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽尝盼。三九已至,卻和暖如春腾啥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背冯吓。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工倘待, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人组贺。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓凸舵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親失尖。 傳聞我的和親對象是個殘疾皇子啊奄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容