Design Patterns Visitor Pattern - Design Patterns

What is Design Patterns Visitor Pattern?

In Visitor pattern, we use a visitor class which modify the execute algorithm of a constituent class. By this way, effecting algorithm of part can vary as and when visitor varies. This pattern comes under behaviour pattern category. As per the pattern, element object has to recognize the visitor object so that visitor object handles the process on the element object.

Implementation

We are going to generate a Computer Part interface defining understand opearation.Keyboard, Mouse, Monitor and Computer are concrete classes implementing Computer Part interface. We will classify another interface ComputerPartVisitor which will define visitor class operations. Computerises concrete visitor to do consequent action.

VisitorPatternDemo, our demo class, will use Computer and ComputerPartVisitor classes to display use of visitor pattern.
visitor_pattern_uml_diagram

Step 1

Define an interface to represent element.

ComputerPart.java

Step 2

Make concrete classes extending the above class.

Keyboard.java

Monitor.java

Mouse.java

Computer.java

Step 3

Describe an interface to represent visitor.

ComputerPartVisitor.java

Step 4

Form concrete company implementing the above class.

ComputerPartDisplayVisitor.java

Step 5

Use the ComputerPartDisplayVisitor to present parts of processor.

VisitorPatternDemo.java

Step 6

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics