/** * 訪問者接口 */ public interface Visitor { /** * 訪問A * @param element */ void visit(ConcreteElementA element); /** * 訪問B * @param element */ void visit(ConcreteElementB element); } /** * 被訪問對象接口 */ public interface Element { void accept(Visitor visitor); String operation(); } /** * 具體的訪問者 */ public class ConcreteVisitor implements Visitor { @Override public void visit(ConcreteElementA element) { System.out.println("visit: " + element.operation()); } @Override public void visit(ConcreteElementB element) { System.out.println("visit: " + element.operation()); } } /** * 具體實例A */ public class ConcreteElementA implements Element { @Override public void accept(Visitor visitor) { visitor.visit(this); } @Override public String operation() { return "ConcreteElementA"; } } /** * 具體實例B */ public class ConcreteElementB implements Element { @Override public void accept(Visitor visitor) { visitor.visit(this); } @Override public String operation() { return "ConcreteElementB"; } }
/** * 測試與應用類 */ public class Test { public static void main(String[] args) { //初始化被訪問列表 List<Element> elements = new ArrayList<>(); //初始化訪問者 Visitor visitor = new ConcreteVisitor(); //初始化元素 Element element1 = new ConcreteElementA(); Element element2 = new ConcreteElementB(); //裝填 elements.add(element1); elements.add(element2); //訪問 elements.forEach(e -> e.accept(visitor)); } }
visit: ConcreteElementA visit: ConcreteElementB
訪問者中的元素html
使用訪問者模式,實現對不一樣種類課程的訪問封裝。
/** * 訪問者接口 */ public interface IVisitor { void visit(FreeCourse freeCourse); void visit(CodingCourse codingCourse); } /** * 訪問者的具體實現 */ public class Visitor implements IVisitor { @Override public void visit(FreeCourse freeCourse) { System.out.println("free course: " + freeCourse.getName()); } @Override public void visit(CodingCourse codingCourse) { System.out.println("coding course: " + codingCourse.getName() + " price: " + codingCourse.getPrice()); } } /** * 抽象類,課程 */ public abstract class Course { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } abstract void accept(IVisitor visitor); } /** * 具體的實體類,免費課程 */ public class FreeCourse extends Course { @Override void accept(IVisitor visitor) { visitor.visit(this); } } /** * 具體的實現類,實戰課程 */ public class CodingCourse extends Course { private int price; public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override void accept(IVisitor visitor) { visitor.visit(this); } }
/** * 測試與應用類 */ public class Test { public static void main(String[] args) { //初始化容器 List<Course> courses = new ArrayList<>(); //構建課程實現 FreeCourse freeCourse = new FreeCourse(); freeCourse.setName("SpringMVC."); CodingCourse codingCourse = new CodingCourse(); codingCourse.setName("Java design pattern."); codingCourse.setPrice(299); //裝填 courses.add(freeCourse); courses.add(codingCourse); //訪問 for (Course course : courses) { course.accept(new Visitor()); } } }
free course: SpringMVC. coding course: Java design pattern. price: 299
訪問者模式和迭代器模式java
慕課網設計模式精講
:https://coding.imooc.com/class/270.html 訪問者模式(Visitor模式)詳解
: http://c.biancheng.net/view/1397.html