Visitor (visitor) JAVA Implementation

Source: Internet
Author: User
Package visitor. mytest; import Java. util. collection; import Java. util. iterator;/*** interviewer * @ author administrator */public abstract class visitor {public void visit (visitable) {// todo auto-generated method stubsystem. out. println (This + "interviewed" + visitable);} public void visitcollection (Collection collection) {iterator it = collection. iterator (); While (it. hasnext () {object OBJ = it. next (); If (OBJ instanceof visitable) {system. out. println (This + "interviewed" + OBJ) ;}}; public abstract string tostring ();}

Package visitor. mytest;/*** interviewee * @ author administrator **/public abstract class visitable {/** interview with the interviewee **/public abstract void accept (visitor );}

Package visitor. mytest; public class visitable1 extends visitable {@ overridepublic void accept (visitor) {visitor. visit (this) ;}@ overridepublic string tostring () {// todo auto-generated method stubreturn "";}}

Package Visitor. myTest; public class Visitable2 extends Visitable {@ Overridepublic void accept (Visitor visitor) {// TODO Auto-generated method stubvisitor. visit (this) ;}@ Overridepublic String toString () {// TODO Auto-generated method stubreturn "respondent B ";}}

Package Visitor. myTest; public class Visitable3 extends Visitable {@ Overridepublic void accept (Visitor visitor) {// TODO Auto-generated method stubvisitor. visit (this) ;}@ Overridepublic String toString () {return "";}}

Package Visitor. MyTest; public class VisitorA extends Visitor {@ Overridepublic String toString () {return "";}}

Package Visitor. MyTest; public class VisitorB extends Visitor {@ Overridepublic String toString () {return "reporter B ";}}

package Visitor.MyTest;import java.util.ArrayList;import java.util.Collection;public class MyTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubVisitable vb1=new Visitable1();Visitable vb2=new Visitable2();Visitable vb3=new Visitable3();Visitor vsa=new VisitorA();Visitor vsb=new VisitorB();vb1.accept(vsa);vsa.visit(vb3);Collection c=new ArrayList();c.add("xx");c.add(vsb);c.add(vb2);vsb.visitCollection(c);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.