Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AF more like Strategy Pattern

 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a couple of different threads, I noticed Ilja Preuss mentioning that the Abstract Factory is more like (or a special form of) Strategy pattern.

I would like to understand this better. I had the following definition of Strategy in my mind: Strategy defines a family of algorithms, encapsulates each one of them, makes them interchangable and lets them vary independent of the client that uses it.

This is what I understood from the Head First Design Patterns. And my interpretation says that, Strategy basically deals with a typical kind of problem of locking some implementation within inheritance hierarchy. By seperating out those implementation (call them algorithm), encapsulating them, and having a composition between the inheritance hierarchy and the algorithm hierarchy solves that problem and helps achieve more flexibility.

Whereas the Abstract Factory deals more with creating instances of family of related classes.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankit Doshi:
Whereas the Abstract Factory deals more with creating instances of family of related classes.


Focus on what is similar:

Abstract Factory deals more with creating instances of family of related classes.
Strategy defines a family of of related algorithms.

A Concrete Factory is one member of the family of factories.
A Concrete Strategy is one member of the family of algorithms.

Abstract Factory is a Creational Pattern
Strategy is a Behavioral pattern.

In the context of the Strategy Pattern "algorithm" is synonymous with "behavior".
The behavior of the Abstract Factory is concerned with creating things.

Therefore is can be argued that the abstract factory is a special case the strategy pattern because:

Abstract Factory defines a family of related "creation behaviors".
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See how similar these are ...

In fact they are so similar we probably wouldn't use both. Maybe we could code the SwingGUIBuilder to know it needs a SwingWidgetFactory ...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words: Abstract Factory is a Strategy where the algorithms are concerned with creating instances.

Does that make sense?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic