I'm a visitor, i would visit the ones i knew.
I'm learning Design Pattern, Here is what i think about the visitor Pattern. if something wrong, please inform me. thanks
//I'm a visitor, i could visit the ones i knew. //if i want to visit a new one, i must know him, and he must accept me first. //the following is Me -- a visitor, and i know He and She, they both accept me. interface Visitor { public void visit(He h); // i know him public void visit(She s); // i know her } class He { public void accept(Visitor me) { // he accepts me me.visit(this); // so i could visit him } } class She { public void accept(Visitor me) { // she accepts me 2 me.visit(this); // so i could visit her } } //and now if i want to visit It, i must know It, that is: interface Visitor { public void visit(He h); // i know him public void visit(She s); // i know her public void visit(It i); // i must know it first } class It { public void accept(Visitor me) { // and it must accept me 2. me.visit(this); // so i could visit it. } } // at this time, i could visit them separately //if i want to visit them one by one, i must know all of them, and each of them must accept me. that is: interface Acceptable { public void accept(Visitor me); } // and each of them is acceptable, so : class He implements Acceptalbe { public void accept(Visitor me) { // he accepts me me.visit(this); // so i could visit him } public void running() { } } class She implements Acceptalbe { public void accept(Visitor me) { // she accepts me 2 me.visit(this); // so i could visit her } public void crying() { } } class It implements Acceptalbe { public void accept(Visitor me) { // and it also accept me. me.visit(this); // so i could visit it. } public void sleeping() { } } //now, i was visiting all the one i knows here: class Visiting { public static void main(String []args) { Visitor me=new Visitor() { // this is the real Me public void visit(He h) { // and i know all of them h.runing(); } public void visit(She s) { s.crying(); } public void visit(It i) { i.sleeping(); } }; Collection me=new LinkedList(); // now i'm ready to visit them me.add(new He()); me.add(new She()); me.add(new It()); Iterator iter = me.iterator(); while ( iter.hasNext() ) { // so i will visit them one by one. Acceptable x=(Acceptable)iter.next(); x.accept(me); // and they all accept me, so i could visit them successful them. } } } <p class="indent"> |
相關文章
- 莎士比亞的『I'm afraid』AI
- 2008 5.9:I think i'm coming down with a cold
- 2008 3 26:I am going to hit the sack,I'm exhaustedGo
- JAVA的i++, i+=1, i=i+1有區別嗎?Java
- I'm shocked that they chose for the City to be the central hub
- 圖解i++和++i圖解
- jvm是如何執行i = i++ + ++i的,你知道嗎?JVM
- c# for(int i;i<3;i++)迴圈的執行次序C#
- Veritas Quick I/O and Cached Quick I/OUI
- java中i=i++的問題Java
- Intel CPU(i3、i5、i7、i9)型號、效能詳細解讀Intel
- Arcaea I
- 關於java中的i++和++iJava
- 計算機I/O與I/O模型計算機模型
- C++複習整理---i++和++iC++
- 位元組碼指令分析 ++i 和 i++
- npm i和npm i --production的區別NPM
- Plan Stability in Oracle 8i/9iOracle
- I/O埠和I/O記憶體記憶體
- Win7 - Administrator登陸後變成Temp用戶Win7
- c語言中i++與++i的使用技巧C語言
- MSN推出i’m符號 聊天越多慈善捐款越多符號
- git rebase -iGit
- I. Disks
- Reflective Journal I
- Java I/OJava
- 在XSL裡執行類似for(i=0;i〈x;i++)的迴圈 (轉)
- synchronized下的 i+=2 和 i++ i++執行結果居然不一樣synchronized
- 處理器i5和i7有什麼區別 電腦i5和i7哪個更好
- i7和i5的處理器哪個好 英特爾i7和i5cpu哪個更好
- rpm的i386、i586和i686版本有什麼不同
- 技術小菜比入坑 LinkedList,i 了 i 了
- Java中 i=i++ 問題底層原理解析Java
- C++中的i++和++i你真的理解嗎?C++
- 每天學點C++知識:用 ++i 替代 i++C++
- Oracle 9i DBA Fundamentals I學習筆記(二)Oracle筆記
- 8i客戶端不能連線9i?客戶端
- 2008.1.3 Chat room? I've heard it but I am not into it.OOM