Visitor Pattern is a Behavioral Pattern used to perform the operations on a group of similar objects. Using Visitor pattern, algorithm can be separated from the object structure.Visitor is an external class and can access the data of another class. Additional features can be added to a class without changing the class.
Advantages
More operations can be performed on objects using Visitor Pattern.
Logical operation of the class can be moved using Visitor Pattern.
Conceptual
figure
IVisitor - This is an interface or a dynamic class used to proclaim the visit operations for every sort of the visitable class. Generally, the name of the operation is the same and the operations are separated by the technique signature. The data object sort choose which strategy is called.
ConcreteVisitor - For any type of guest, all the visit techniques announced in theoretical guest must be actualized. Every Guest will be in charge of various operations. When another guest is characterized, it must be gone to the article structure.
IElement - is a deliberation that announces the acknowledge operation. This is the passage point which empowers an article to be "went by" by the guest object. Every article from a gathering should execute this reflection with a specific end goal to have the capacity to be gone to.
ConcreteElement - Classes that execute the Visitor interface or class characterizes the acknowledge operation. The visitor article is gone to this item utilizing the acknowledge operation.
Examples
Creating an interface Sports.
[java]public interface Sports
{
public void accept(SportsName sportsName);
}[/java]
Creating a class Cricket which implements Sports.
[java]
public class Cricket implements Sports
{
public void accept(SportsName sportsName)//overrides the interface method
{
sportsName.visit(this);
}
}
[/java]
Creating a class Football which implements Sports.
[java]
public class Football implements Sports
{
public void accept(SportsName sportsName)//overrides the interface method
{
sportsName.visit(this);
}
}
[/java]
Computer class implements Sports
[java]
public class Computer implements Sports
{
Sports[] sports;
public Computer()
{
sports = new Sports[] {new Cricket(), new Football()};
}
public void accept(SportsName sportsName) //overrides the interface method
{
for (int i = 0; i < parts.length; i++)
{
parts[i].accept(sportsName);
}
sportsName.visit(this);
}
}[/java]
Creating iinterface SportsPartVisitor.
[java]
public interface SportsPartVisitor
{
public void visit(Computer computer);
public void visit(Cricket cricket);
public void visit(Football football);
}[/java]
SportspartDisplayVisitor class implements SportsPartVisitor.
[java]
public class SportspartDisplayVisitor implements SportsPartVisitor
{
public void visit(Computer computer)//overrides the interface method
System.out.println("Displaying Computer.");
}
public void visit(Cricket cricket)//overrides the interface method
{
System.out.println("Displaying Cricket .");
}
public void visit(Football football)//overrides the interface method
{
System.out.println("Displaying Football.");
}
}[/java]
VisitorPattern class defines the useage of visitorpattern.
[java]
public class VisitorPattern
{
public static void main(String[] args)
{
SportsPart computer = new Sports();//creates object for SportsPart class
computer.accept(new SportsPartDisplayVisitor());
}
}[/java]
Output
The result will be as follows.
[java]
Displaying Computer.
Displaying Cricket.
Displaying Football.
[/java]
Summary
Key Points
Adding new features to a system is easy.
Loose coupling of objects can be done easily in visitor pattern.