Home  /  Questions  /  Question



100   96.6
May 18, 2010


Difference between Decorator and Visitor

What is the difference between Decorator and Visitor pattern?
I find that the purpose of both pattern is similar to an extent.



80   96.4
May 19, 2010
What difference i can see is ,
Visitor pattern is aware of types of visitors which can visit, and decorator is not aware of new behaviours which will be added in future.
I know that this is not very technical way of explaining and i may be even wrong.
Everybody is invited with their views.
regards,

May 23, 2010
Decorator:

Allows you to add functionality to various methods by supporting wrapper classes, without having to modify or edit your existing class.
It uses a wrapper to customize or decorate the functionality of original class, without making any code change to the original class.


Visitor:

If you have a complex tree of objects (sturcture of classes using inheritance) and if you want to add an operation to each of these objects, you need not necessariliy add an operation to each of these classes. Instead you can send a visitor to this sturcture of classes and collect the necessary data from all objects. Then you can invoke the necessary operation on the visitor class. Visitor is always paired up with traverser object to move through all the objects in the tree of objects.


Key differences:

The key difference I see here are as below.

1. Visitor has a tree of objects to be worked upon. Whereas decorator may have just a single object to customize.
2. Visitor requires a traverser for successful implementation.


50   50
Jul 12, 2010
As i saw that Decorator pattern  is a structural pattern that help us to add new function to an object in the run time , note that in the run time not design time . But Visitor pattern is Behavioral pattern that seperate the data structure from the operation (functionality ) that work on it , this mean we can add different operation on the same data structure  . i hope this is help SaebNajim