Under review

Strategy pattern vs abstract factory

Alex Bart 3 years ago updated by Alexander Shvets 3 years ago 3

Hello all,

I'm trying to understand what the difference between strategy pattern and abstract factory, they both basically do the same thing.
They both retrieve an object that can be executed in a different variation, just a different implementation tactic.
Can someone please help on the issue maybe with an close example.
Under review

Hi Alex!

There are indeed a lot of similarities between various patterns in terms of implementation. However, please note that a pattern is not just about the code. It's also about intention and vocabulary.

When you tell your colleague that you have implemented an abstract factory, they immediately get a clue not only about implementation but also about the reason why did you write this code at all—they think that you had several families of products, and you needed a way to manage the creation of these objects. You don't get all of this subtle info when you say that you implemented a Strategy. On the other hand, when you say that you applied Strategy to object creation, I suppose that you implemented several different ways to create some specific object, rather than create various kinds of stuff (which would be correct for Abstract Factory).

Does this make sense?

Thanks for the answer, it does make sense :)

Just to make sure I understand it right the main difference it is on the return object.

They both will use an interface for the object creation.

Factory returns a "simple" object usually a type of something, can be an animal(cat,dog...) Or maybe employee type.

Strategy will return an algorithm that does something, maybe two different installation process or analyzing method.


Yeah, that sounds all right.