Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Visitor Pattern

No description
by

Vlad Gheorghincă

on 3 October 2012

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Visitor Pattern

Visitor Pattern visitor design pattern is a way of separating an algorithm from an object structure on which it operates Object (Visitor)

code for triangle

code for rectangle

code for apple

code for car
code Example perform operations on these objects code code code many classes of objects class Triangle {
.....
public void accept(Visitor aVisitor){
aVisior.visit(this);
}
} functionality operations (i.e. algorithms), which are separated from the class hierarchy on which those algorithms operates class Visitor1{

public void visit(Triangle aTriangle){
//code to modify aTriangle
...
}

public void visit(Rectangle aRect){
//code to modify aRect
...
}

} class Rectangle {
.....
public void accept(Visitor aVisitor){
aVisior.visit(this);
}
} Visitor1 myVisitor = new Visitor1();
Triangle myTriangle = new Triangle();
Rectangle myRectangle = new Rectangle();
myTriangle.accept(myVisitor);
myRectangle.accept(myVisitor);
// myTriangle and my Rectangle are changed now... so... when to use? the classes defining the object structure rarely change, but you often want to define new operations over the structure (e.g. Visitor1, Visitor2). Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes. when is not appropriate ? Avoid "polluting" their classes with these operations.
Visitor lets you keep related operations together by defining them in one class. Composite Visitors can be used to apply an operation over an object
Interpreter Visitor may be applied to do the interpretation Related Patterns Thank you Vlad Gheorghinca for your attention !
Full transcript