Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between visitor and strategy pattern?

I have learned both the patterns but did not understand the differences between these two patterns.

I do not know scenarios, when and where to use these patterns.

Can any one explain the differences and use cases?

like image 728
swaminathan_manickam Avatar asked Nov 28 '12 13:11

swaminathan_manickam


People also ask

What is difference between Bridge and strategy pattern?

Strategy Pattern is used for Behavioural decisions, while Bridge Pattern is used for Structural decisions. Brigde Pattern separats the abstract elements from the implementation details, while Strategy Pattern is concerned making algorithms more interchangeable.

What is difference between strategy pattern and command pattern?

Command pattern does some action on the information stored in the command object, while the Strategy pattern decides how to process the given object. A common example of the Strategy pattern implementing a sort function for a list of numbers. A different sorting method may be used for a list of different sizes.

What is the difference between pattern and strategies?

State pattern helps a class to exhibit different behaviors in a different state. Strategy Pattern encapsulates a set of related algorithms and allows the client to use interchangeable behaviors through composition and delegation at runtime.

What does a visitor pattern do?

In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying the structures.


1 Answers

The main difference is that the Strategy Pattern encapsulates a single group of related behaviors, while the Visitor Pattern encapsulates multiple such groups.

  • You should use the Strategy Pattern when you need to encapsulate a behavior - If you have a family of algorithms and you need to choose among them at run time, you should use Strategy Pattern. This is very common: it happens every time you program to an interface.
  • You should use the Visitor Pattern to implement double dispatch - If you have a group of algorithms that need to be virtual in relation to more than one object. This is far less common, in part because it is much harder to implement.
like image 170
Sergey Kalinichenko Avatar answered Sep 21 '22 11:09

Sergey Kalinichenko