Wednesday, January 1, 2014

Strategy Pattern

Allows behavior of an algorithm to be selected at run time. The strategy pattern, 
  • defines family of algorithms,
  • encapsulate each algorithm and
  • makes algorithms interchangeable




Following is an example of a implementing strategy pattern.




Therefore the host class (The class that uses strategy, below the Context) class doesn't have to implement the details of the strategy. It will only use the needed strategy.

Difference with State Pattern

The strategy pattern deals with how an object performs a certain task. Encapsulating the implementation. Where as The state pattern deal with what state the object is in, wrapping state behavior. Other than that strategy pattern and state pattern are very similar.

Difference with Factory Pattern (DZone)

Strategy pattern may look very similar to factory but they are different. When using strategy if you take above example, Context just have a strategy property which gets injected. But if you use Factory, specifically a ContextFactory, The type is not generic anymore. Your implementation would a Either BusContext or CaraContext. 


Resources

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook