文章詳情頁
java設(shè)計(jì)模式之Visitor
瀏覽:32日期:2024-06-27 10:00:51
內(nèi)容: Visitor定義作用于某個(gè)對象群中各個(gè)對象的操作. 它可以使你在不改變這些對象本身的情況下,定義作用于這些對象的新操作.在Java中,Visitor模式實(shí)際上是分離了collection結(jié)構(gòu)中的元素和對這些元素進(jìn)行操作的行為.為何使用Visitor?Java的Collection(包括Vector和Hashtable)是我們最經(jīng)常使用的技術(shù),可是Collection好象是個(gè)黑色大染缸,本來有各種鮮明類型特征的對象一旦放入后,再取出時(shí),這些類型就消失了.那么我們勢必要用If來判斷,如:Iterator iterator = collection.iterator()while (iterator.hasNext()) { Object o = iterator.next(); if (o instanceof Collection) messyPrintCollection((Collection)o); else if (o instanceof String) System.out.println('''+o.toString()+'''); else if (o instanceof Float) System.out.println(o.toString()+'f'); else System.out.println(o.toString());}在上例中,我們使用了 instanceof來判斷 o的類型.很顯然,這樣做的缺點(diǎn)代碼If else if 很繁瑣.我們就可以使用Visitor模式解決它.如何使用Visitor?針對上例,我們設(shè)計(jì)一個(gè)接口visitor訪問者:public interface Visitor{ public void visitCollection(Collection collection); public void visitString(String string); public void visitFloat(Float float);} 在這個(gè)接口中,將我們認(rèn)為Collection有可能的類的類型放入其中.有了訪問者,我們需要被訪問者,被訪問者就是我們Collection的每個(gè)元素Element,我們要為這些Element定義一個(gè)可以接受訪問的接口(訪問和被訪問是互動(dòng)的,只有訪問者,被訪問者如果表示不歡迎,訪問者就不能訪問),我們定義這個(gè)接口叫Visitable,用來定義一個(gè)Accept操作,也就是說讓Collection每個(gè)元素具備可訪問性.public interface Visitable{ public void accept(Visitor visitor);} 好了,有了兩個(gè)接口,我們就要定義他們的具體實(shí)現(xiàn)(Concrete class):public class ConcreteElement implements Visitable{ private String value; public ConcreteElement(String string) { value = string; } //定義accept的具體內(nèi)容 這里是很簡單的一句調(diào)用 public void accept(Visitor visitor) { visitor.visitString(this); }} 再看看訪問者的Concrete實(shí)現(xiàn):public class ConcreteVisitor implements Visitor{ //在本方法中,我們實(shí)現(xiàn)了對Collection的元素的成功訪問 public void visitCollection(Collection collection) { Iterator iterator = collection.iterator() while (iterator.hasNext()) { Object o = iterator.next(); if (o instanceof Visitable) ((Visitable)o).accept(this); } public void visitString(String string) { System.out.println('''+string+'''); } public void visitFloat(Float float) { System.out.println(float.toString()+'f'); }} 在上面的visitCollection我們實(shí)現(xiàn)了對Collection每個(gè)元素訪問,只使用了一個(gè)判斷語句,只要判斷其是否可以訪問.至此,我們完成了Visitor模式基本架構(gòu).使用Visitor模式的前提對象群結(jié)構(gòu)中(Collection) 中的對象類型很少改變,也就是說訪問者的身份類型很少改變,如上面中Visitor中的類型很少改變,如果需要增加新的操作,比如上例中我們在ConcreteElement具體實(shí)現(xiàn)外,還需要新的ConcreteElement2 ConcreteElement3.可見使用Visitor模式是有前提的,在兩個(gè)接口Visitor和Visitable中,確保Visitor很少變化,變化的是Visitable,這樣使用Visitor最方便.如果Visitor也經(jīng)常變化, 也就是說,對象群中的對象類型經(jīng)常改變,一般建議是,不如在這些對象類中逐個(gè)定義操作.但是Java的Reflect技術(shù)解決了這個(gè)問題.Reflect技術(shù)是在運(yùn)行期間動(dòng)態(tài)獲取對象類型和方法的一種技術(shù),具體實(shí)現(xiàn)參考Javaworld的英文原文. Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
標(biāo)簽:
Java
相關(guān)文章:
1. php面向?qū)ο蟪绦蛟O(shè)計(jì)介紹2. JavaScript設(shè)計(jì)模式之策略模式實(shí)現(xiàn)原理詳解3. JS中間件設(shè)計(jì)模式的深入探討與實(shí)例分析4. PHP設(shè)計(jì)模式之解釋器模式淺析5. 深入分析PHP設(shè)計(jì)模式6. WMLScript腳本程序設(shè)計(jì)第1/9頁7. 秒殺場景的緩存、隊(duì)列、鎖使用Redis優(yōu)化設(shè)計(jì)方案8. 秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法9. php設(shè)計(jì)模式之職責(zé)鏈模式實(shí)例分析【星際爭霸游戲案例】10. php面向?qū)ο蟪绦蛟O(shè)計(jì)
排行榜
