- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Visitor Pattern
In Visitor pattern, we use a visitor class which changes the executing algorithm of an element class. By this way, execution algorithm of element can vary as and when visitor varies. This pattern comes under behavior pattern category. As per the pattern, element object has to accept the visitor object so that visitor object handles the operation on the element object.
Implementation
We are going to create a ComputerPart interface defining accept opearation.Keyboard, Mouse, Monitor and Computer are concrete classes implementing ComputerPart interface. We will define another interface ComputerPartVisitor which will define a visitor class operations. Computer uses concrete visitor to do corresponding action.
VisitorPatternDemo, our demo class, will use Computer and ComputerPartVisitor classes to demonstrate use of visitor pattern.
Step 1
Define an interface to represent element.
ComputerPart.java
pubpc interface ComputerPart { pubpc void accept(ComputerPartVisitor computerPartVisitor); }
Step 2
Create concrete classes extending the above class.
Keyboard.java
pubpc class Keyboard implements ComputerPart { @Override pubpc void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } }
Monitor.java
pubpc class Monitor implements ComputerPart { @Override pubpc void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } }
Mouse.java
pubpc class Mouse implements ComputerPart { @Override pubpc void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } }
Computer.java
pubpc class Computer implements ComputerPart { ComputerPart[] parts; pubpc Computer(){ parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()}; } @Override pubpc void accept(ComputerPartVisitor computerPartVisitor) { for (int i = 0; i < parts.length; i++) { parts[i].accept(computerPartVisitor); } computerPartVisitor.visit(this); } }
Step 3
Define an interface to represent visitor.
ComputerPartVisitor.java
pubpc interface ComputerPartVisitor { pubpc void visit(Computer computer); pubpc void visit(Mouse mouse); pubpc void visit(Keyboard keyboard); pubpc void visit(Monitor monitor); }
Step 4
Create concrete visitor implementing the above class.
ComputerPartDisplayVisitor.java
pubpc class ComputerPartDisplayVisitor implements ComputerPartVisitor { @Override pubpc void visit(Computer computer) { System.out.println("Displaying Computer."); } @Override pubpc void visit(Mouse mouse) { System.out.println("Displaying Mouse."); } @Override pubpc void visit(Keyboard keyboard) { System.out.println("Displaying Keyboard."); } @Override pubpc void visit(Monitor monitor) { System.out.println("Displaying Monitor."); } }
Step 5
Use the ComputerPartDisplayVisitor to display parts of Computer.
VisitorPatternDemo.java
pubpc class VisitorPatternDemo { pubpc static void main(String[] args) { ComputerPart computer = new Computer(); computer.accept(new ComputerPartDisplayVisitor()); } }
Step 6
Verify the output.
Displaying Mouse. Displaying Keyboard. Displaying Monitor. Displaying Computer.Advertisements