• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Visitor vs. Strategy

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone pls sheed some light on the difference between the strategy pattern & the visitor? My brain seems to be zoned in on the strategy patterns, b/c whenever i try to learn a new pattern, i say to myself "this really looks like the strategy pattern".

Thanks all!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Visitor is like an extended Strategy that is polymorphic at two dimensions: the code executed doesn't only depend on the actual strategy used, but also on the parameter passed to it.

Strategy actually *is* a very basic pattern that can be seen as the foundation of many other patterns. Abstract Factory, for example, is a Strategy for creating objects. So your feeling isn't totally wrong!

The patterns that build on Strategy do have value, though, because they have a more specific intent, and therefore also can give more specific advice than Strategy alone could give.
 
Kristian Paul
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense, thank you!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strategy, State, Command, Visitor (and more I'm sure) all have some behavior built into an object that we can pass around, so they all have a very similar feeling when you're building the object. Their "intent" is subtly different and yet sometimes overlapping.

Visitor is often (but not always) a way to add new functionality to an existing object graph. The example I learned on is an HTML parser that builds a DOM of objects on an HTML page and invites me to add functionality via visitors. I can do that without modifying the DOM objects.

I use Strategy a lot to handle inputs differently based on some attribute. Say a method expects an Appliance argument and does special work on Toasters, Blenders, Microwaves, etc. I might have a long case statement to test for each type and do the right thing, or just get the right Strategy from a Map and execute it. (Of course the better solution is to build polymorphic behavior into the parameter objects, but for reasons too long to explain today I couldn't.)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about the diffs here:

web page
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic