eaglewap Data Scientist | Software Engineer | Google Code U Alumni | Former WSO2 Software Engineering Intern | "Google Code U"Alumni | Former WSO2 Software Engineering Intern. No comments

Visitor Design Pattern

  • Collections contain objects of different types and in those cases some operations have to be performed on all the collection elements without knowing the type
  • –Checking for instance type is not elegant
  • Visitor pattern represents an operation to be performed on the elements of an object structure

Class diagram for Visitor Design Pattern


Visitor

Defines a new operation to a class without change

Example Java Code for Visitor Design Pattern 😊

This is the scenario where Doctor visits school and test every child in the school . Its like going through every objects and perform an operation.
Visitor.java
1
2
3
public interface Visitor {
    public void visit(Visitable visitable);
}
Doctor.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Doctor implements Visitor {

    private String name;

    public Doctor(String name) {
        this.name = name;
    }

    @Override
    public void visit(Visitable visitable) {
        Kid child = (Kid) visitable;
        child.setHealthStatus("bad");
        System.out.println("Child Specialist visited " + child.getName() + " set the status to: " + child.getHealthStatus());

    }

}
Visitable.java
1
2
3
public interface Visitable {
    public void accept (Visitor visitor);
}
Kid.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Kid implements Visitable {

    private String name;
    private String healthStatus;

    public Kid(String name, String healthStatus) {
        super();
        this.name = name;
        this.healthStatus = healthStatus;
    }

    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setHealthStatus(String healthStatus) {
        this.healthStatus = healthStatus;
    }

    public String getHealthStatus() {
        return healthStatus;
    }

}
School.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class School {
    static ArrayList<Kid> childlist;
    
    public static void doHealthChckup(Visitor visitor){
        
        for (Kid child:childlist){
            child.accept(visitor);
        }
    
    }
}
Client.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Demo {
    public static void main(String[] args) {
        Kid ram =new Kid("Ram","Good");
        Kid ramesh =new Kid("Ramesh","Good");
        Kid ramu =new Kid("Ramu","Good");
        Kid ramar =new Kid("Ramar","Good");
        Kid ramei =new Kid("Ramie","Good");
        School.childlist=new ArrayList<>();
        School.childlist.add(ram);
        School.childlist.add(ramu);
        School.childlist.add(ramei);
        School.childlist.add(ramu);
        Visitor kadavul =new Doctor("kadavul");
        School.doHealthChckup(kadavul);
        
    }
}

Comments (0)

Post a Comment

Cancel