Visitor Design Pattern
Question : - What
is Visitor Design Pattern?
Answer : - This
design pattern also falls under Behavioral
design pattern.
Using this design pattern, we can move the operational logic from the
objects to another class. This pattern is providing a way of
separating an algorithm from an object structure on which it
operates. This visitor pattern is used to simplify operations on
groupings of related objects.
As
of GOF:-“Allows
for one or more operation to be applied to a set of objects at
runtime, decoupling the operations from the object structure.”
Benefits
: -
Adding
new operatios are easy because if the logic of operation changes,
then we need to make change only in the visitor implementation rather
than doing it in all the item classes.
Visitors
can gather state as they visit each element in the object structure.
Without a visitor, this state would have to be passed as an extra
arguments to the operations that perform the traversal.
Disadvantage
: -
During
design period of application, we should know the return type of
visit() methods otherwise
we
will have to change the interface and all of its implementations.
If
there are many implementations of visitor interface then extending
that class is not a easy task.
For more information on Visitor design pattern please refer to the below link
In the below example, we will see the implementation of the Visitor design pattern
BodyPart.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public interface BodyPart {
public void accept(BodyPartVisitor bodyPartVisitor);
}
Body.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public class Body implements BodyPart{
private BodyPart[] bodyParts;
public Body(){
bodyParts = new BodyPart[] {new Eye(), new Heart(), new Kidney()};
}
@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
for (int i = 0; i < bodyParts.length; i++) {
bodyParts[i].accept(bodyPartVisitor);
}
bodyPartVisitor.visit(this);
}
}
BodyPartVisitor.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public interface BodyPartVisitor {
public void visit(Body body);
public void visit(Eye eye);
public void visit(Heart heart);
public void visit(Kidney kidney);
}
Eye.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public class Eye implements BodyPart{
@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}
}
Heart.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public class Heart implements BodyPart{
@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}
}
Kidney.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public class Kidney implements BodyPart {
@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}
}
FullBodyScanner.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public class FullBodyScanner implements BodyPartVisitor{
@Override
public void visit(Body body) {
System.out.println("Scanning Body");
}
@Override
public void visit(Eye eye) {
System.out.println("Scanning Eye");
}
@Override
public void visit(Heart heart) {
System.out.println("Scanning Heart");
}
@Override
public void visit(Kidney kidney) {
System.out.println("Scanning Heart");
}
}
VisitorDesignPatternDemo.java
package com.gaurav.designpattern.visitor;
/**
* @author Gaurav
*
*/
public class VisitorDesignPatternDemo {
public static void main(String args[]) {
BodyPart body = new Body();
body.accept(new FullBodyScanner());
}
}
Result:-
Scanning Eye
Scanning Heart
Scanning Kidney
Scanning Body
No comments:
Post a Comment