标签:etl new for 操作 list extend something print 不同
访问者模式:为某个对象组合添加新功能,而又不改变组合的内部结构
特点:
举例:
1 abstract class Element { 2 public abstract void accept(IVisitor visitor); 3 public abstract void doSomething(); 4 } 5 6 interface IVisitor { 7 public void visit(Element e); 8 } 9 10 class ConcreteElement extends Element { 11 public void doSomething(){ 12 System.out.println("这是元素"); 13 } 14 15 public void accept(IVisitor visitor) { 16 visitor.visit(this); 17 } 18 } 19 20 class Visitor implements IVisitor { 21 22 public void visit(Element e) { 23 e.doSomething(); 24 } 25 } 26 27 28 public class Client { 29 public static void main(String[] args){ 30 List<Element> list = ObjectStruture.getList(); 31 for(Element e: list){ 32 e.accept(new Visitor()); 33 } 34 } 35 }
标签:etl new for 操作 list extend something print 不同
原文地址:http://www.cnblogs.com/HectorHou/p/6049353.html