Skip to content

Commit

Permalink
Visitor design pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
bhowmikp committed May 5, 2018
1 parent a0ab57f commit 7c48bea
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 1 deletion.
10 changes: 10 additions & 0 deletions behavioral_patterns/visitor/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Client {

public static void main(String[] args) {

ComputerPart computer = new Computer();
computer.accept(new ComputerPartDisplayVisitor());

}

}
18 changes: 18 additions & 0 deletions behavioral_patterns/visitor/Computer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Computer implements ComputerPart {

ComputerPart[] parts;

public Computer() {

parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()};

}

public void accept(ComputerPartVisitor computerPartVisitor) {
for (int i = 0; i < parts.length; i++) {
parts[i].accept(computerPartVisitor);
}
computerPartVisitor.visit(this);
}

}
5 changes: 5 additions & 0 deletions behavioral_patterns/visitor/ComputerPart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public interface ComputerPart {

public void accept(ComputerPartVisitor computerPartVisitor);

}
19 changes: 19 additions & 0 deletions behavioral_patterns/visitor/ComputerPartDisplayVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class ComputerPartDisplayVisitor implements ComputerPartVisitor {

public void visit(Computer computer) {
System.out.println("Displaying Computer.");
}

public void visit(Mouse mouse) {
System.out.println("Displaying Mouse.");
}

public void visit(Keyboard keyboard) {
System.out.println("Displaying Keyboard.");
}

public void visit(Monitor monitor) {
System.out.println("Displaying Monitor.");
}

}
8 changes: 8 additions & 0 deletions behavioral_patterns/visitor/ComputerPartVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public interface ComputerPartVisitor {

public void visit(Computer computer);
public void visit(Mouse mouse);
public void visit(Keyboard keyboard);
public void visit(Monitor monitor);

}
7 changes: 7 additions & 0 deletions behavioral_patterns/visitor/Keyboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Keyboard implements ComputerPart {

public void accept(ComputerPartVisitor computerPartVisitor) {
computerPartVisitor.visit(this);
}

}
7 changes: 7 additions & 0 deletions behavioral_patterns/visitor/Monitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Monitor implements ComputerPart {

public void accept(ComputerPartVisitor computerPartVisitor) {
computerPartVisitor.visit(this);
}

}
7 changes: 7 additions & 0 deletions behavioral_patterns/visitor/Mouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Mouse implements ComputerPart {

public void accept(ComputerPartVisitor computerPartVisitor) {
computerPartVisitor.visit(this);
}

}
33 changes: 32 additions & 1 deletion behavioral_patterns/visitor/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# Visitor

Defines a new operation to a class without change
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

**Frequency of Use** - 1/5

## UML Class Diagram
![Visitor UML](visitor.gif)

## Participants
The classes and objects participating in this pattern are:
- Visitor (ComputerPartVisitor)
- declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the element directly through its particular interface.
- ConcreteVisitor (ComputerPartDisplayVisitor)
- implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.
- Element (ComputerPart)
- defines an Accept operation that takes a visitor as an argument.
- ConcreteElement (Keyboard, Monitor, Mouse, Computer)
- implements an Accept operation that takes a visitor as an argument.
- ObjectStructure ()
- can enumerate its elements.
- may provide a high-level interface to allow the visitor to visit its elements.
- may either be a composite or a collection such as a list or a set.

## Applicability
Use the Visitor pattern when:
- an object contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes.
- many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations.
- the classes defining the object structure rarely change, but you often want to define new operations over structure.

## Benefits
- **Visitor makes adding new operations easy**. Simply add a new visitor.
- **A visitor gathers related operations and seperates unrelated ones**. Related behaviour isn't spread over the classes defining the object structure; it's localized in a visitor. Unrelated sets of behaviour are partitioned in their own visitor subclasses.
- **Visiting across class hierarchies**. Visitors can visit objects that don't have a common parent class.
Binary file added behavioral_patterns/visitor/visitor.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7c48bea

Please sign in to comment.