If you look both deal with multiple objects so why we have two patterns for one thing? What is the difference in both of them? I have looked both of them at Gang of Four book.
-
possible duplicate of [What is the difference between Factory and Strategy patterns?](http://stackoverflow.com/questions/616796/what-is-the-difference-between-factory-and-strategy-patterns) – wheaties Oct 27 '13 at 14:59
-
I prefer to not stress over this. An OO language, and especially Java only has so many constructs you can use when **implementing** a pattern - you can't really divorce how they look in code (where there'll necessarily be overlap) from their context and what they're used for. (Which isn't "dealing with multiple objects".) On the other side of the spectrum you have languages with functional programming features, where many of these patterns [become invisible](http://norvig.com/design-patterns/design-patterns.pdf) - factories, strategies, and commands can be all implemented using lambdas. – millimoose Oct 27 '13 at 15:00
3 Answers
If you have studied GoF book then study difference of Creational and Operational pattern. Also see What is the difference between Factory and Strategy patterns?
-
3Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – John Dvorak Oct 27 '13 at 14:59
I'm surprised this question comes up so often since Factory and Strategy are in two different categories entirely. Factory is creational; Strategy is operational. I don't need to explain further since Stack Overflow legend @tvanfosson created this excellent post on the matter.
Bear in mind also that the two can coexist simultaneously. You might have a factory that produces strategies that can be injected into a class.
The far more interesting question is the difference between Strategy and Template Method, two similar patterns that differ in one significant way. That gets to the whole discussion of inheritance vs. delegation that is a really good thing for OO developers to understand.
-
1Some essentials: IMO well said @Vidya Factory is just an approach for decoupling the dependent objects on which an instance depends. While in Factory, a strategy can be use during creation to select on "runtime" among the strategies for the creation. – khunshan Oct 27 '13 at 15:29
-
I think the confusion comes from the fact that both tend to use interfaces to do a job. In a manner of speaking, you might implement a factory with a strategy pattern. – Erik Funkenbusch Oct 27 '13 at 15:51
-
The second point is certainly true. Because they do different things, they can coexist either way. But interfaces are a huge part of all the patterns. – Vidya Oct 27 '13 at 16:12
You cannot understand the difference simply by looking at the code or categorization. To grasp the GoF patterns correctly, look for their intents:
Strategy: "Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it."
Factory Method: "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."
And here's an explanation about them: Difference between Factory Method and Strategy design patterns