wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes AF more like Strategy Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "AF more like Strategy Pattern" Watch "AF more like Strategy Pattern" New topic
Author

AF more like Strategy Pattern

Ankit Doshi
Ranch Hand

Joined: Dec 04, 2002
Posts: 222
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

Joined: Aug 19, 2005
Posts: 2922
    
    5
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

Joined: Jan 29, 2003
Posts: 8791
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 ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words: Abstract Factory is a Strategy where the algorithms are concerned with creating instances.

Does that make sense?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AF more like Strategy Pattern