-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
behavioral_patterns/visitor/ComputerPartDisplayVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
 | ||
|
||
## 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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.