0

I read about visitor pattern at http://en.wikipedia.org/wiki/Visitor_pattern

Initial understanding of this pattern created impression that visitor pattern is same as Bridge/Strategy pattern.

So is that specific example creating such impression ? Can someone explain the difference; possibaly with unambiguous example in Java ?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Kaushik Lele
  • 6,439
  • 13
  • 50
  • 76
  • Could moved to programmers.stackexchange.com – Berkay Turancı Feb 04 '13 at 08:26
  • 2
    I've never quite understood the difference either; I don't think this is deserving of down-votes - and, if you are going to down-vote, please kick in a close-vote as well. (Unfortunately, understanding lots of named "patterns" seems to be a "practical requirement" when dealing with "enterprise code".) –  Feb 04 '13 at 08:27
  • In short, when you are studying DP, first thing you need to know is "what problem this DP is solving". Visitor and Strategy is solving fundamentally different problem (changing implementation without caller awareness VS processing object structure with a algorithm provided). Although the outcome may look similar, they are fundamentally different and you can see the difference by the way it is used. – Adrian Shum Feb 04 '13 at 08:33
  • @Ray Consider example in "http://en.wikipedia.org/wiki/Visitor_pattern" As you quoted "Visitor lets you define a new operation without changing the classes of the elements on which it operates.". Do you mean Visitor inteface has visit() method. Now you can add visit2(), visit3() etc. methods without changing CarElement child i.e. classes on which it operates ? Am I correct ? But then who will call these new methods without changing accept() method. – Kaushik Lele Feb 04 '13 at 09:10

2 Answers2

1

From the GOF book, the intents are very different:

Visitor - Object Behavioral - Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Bridge - Object Structural - Decouple an abstraction from its implementation so that the two can vary independently.

Strategy - Object Behavioral - Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

There are lots of Java example around.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0

Bridge:

Bridge decouples abstraction from implementation. Both abstraction and implementation can vary independently.

  1. Create bridge implementer interface
  2. Create concrete bridge implementer classes
  3. Create an abstract class which contains the interface ( Composition is key here)
  4. Create concrete class implementing the abstract class

If you are looking for examples, have a look at tutorialspoint article.

Visitor:

Visitor lets you define a new operation without changing the classes of the elements on which it operates.

From GOF definition:

Allows for one or more operation to be applied to a set of objects at runtime, decoupling the operations from the object structure.

The implementation proceeds as follows.

  1. Create a Visitor class hierarchy that defines a pure virtual visit() method in the abstract base class for each concrete derived class in the aggregate node hierarchy.
  2. Each visit() method accepts a single argument - a pointer or reference to an original Element derived class.
  3. Each operation to be supported is modeled with a concrete derived class of the Visitor hierarchy. The visit() methods declared in the Visitor base class are now defined in each derived subclass by allocating the "type query and cast" code in the original implementation to the appropriate overloaded visit() method.
  4. Add a single pure virtual accept() method to the base class of the Element hierarchy

For working java code, have a look at tutorials point article and dzone article.

Strategy:

Strategy allows to switch from one algorithm to other algorithm (from a family of algorithms) dynamically at run time.

Have a look at this SE question for more details.

Real World Example of the Strategy Pattern

Community
  • 1
  • 1
Aditya W
  • 652
  • 8
  • 20