New丶Elements 2019-06-27
访问者模式
public interface IElement { void accept(IVisitor visitor); }
public class ElementA implements IElement { @Override public void accept(IVisitor visitor) { visitor.visit(this); } } public class ElementB implements IElement { @Override public void accept(IVisitor visitor) { visitor.visit(this); } }
public interface IVisitor { void visit(ElementA element); void visit(ElementB element); }
public class Visitor implements IVisitor { @Override public void visit(ElementA element) { System.out.println("ElementA visit ... "); } @Override public void visit(ElementB element) { System.out.println("ElementB visit ... "); } }
public static void main(String[] args) { IElement element = new ElementA(); IVisitor visitor = new Visitor(); element.accept(visitor); element = new ElementB(); element.accept(visitor); }
ElementA visit ... ElementB visit ...
https://github.com/Seasons20/DisignPattern.git
END