Back to list
Views:   23.4K
Replies:  3

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.
Saurabh Saxena, May 18, 2010
Reply 1
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
Saeb Najim, Jul 12, 2010
Reply 2

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.


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.

Sreenivas Manyam Rajaram, May 23, 2010
Reply 3
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.
Ram Pandey, May 19, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.