首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计形式之访问(visitor)

2012-09-09 
设计模式之访问(visitor)public class VisitorDemo {public static void main(String[] args) {Collection

设计模式之访问(visitor)

public class VisitorDemo {public static void main(String[] args) {Collection c = new ArrayList();c.add(new StringElement("string1"));c.add(new StringElement("string2"));c.add(new FloatElement(1.0f));c.add(new FloatElement(2.0f));Visitor v = new ConcreteVisitor();v.visitCollection(c);}}interface Visitor{void visitStringElement(StringElement stringElement);void visitFloatElement(FloatElement floatElement);void visitCollection(Collection collection);}class ConcreteVisitor implements Visitor{public void visitCollection(Collection collection) {Iterator iter = collection.iterator();while(iter.hasNext()){Object obj = iter.next();if(obj instanceof Visitable){((Visitable) obj).accept(this);}}}public void visitFloatElement(FloatElement floatElement) {System.out.println(floatElement.getValue());}public void visitStringElement(StringElement stringElement) {System.out.println(stringElement.getValue());}}
?//http://fengzl.iteye.com/blog/114758

热点排行