Can someone once and for all explain to me the difference between these two and try to give a sort of guideline for when to use each one of them? Examples would be really nice.
Factory Method pattern is responsible for creating products that belong to one family, while Abstract Factory pattern deals with multiple families of products. Factory Method uses interfaces and abstract classes to decouple the client from the generator class and the resulting products.
But, the key difference is that Strategy Pattern is about modifying a behaviour of a context in runtime using strategies, while Template Method Pattern is about following a skeleton implementation of an algorithm and modifying its behaviour by overriding methods of the skeleton class in the subclasses.
Abstract Factory pattern uses composition to delegate the responsibility of creating an object to another class while Factory Method design pattern uses inheritance and relies on a derived class or subclass to create an object.
The strategy pattern allows you to change the implementation of something used at runtime. The decorator pattern allows you augment (or add to) existing functionality with additional functionality at run time.
The INTENT is different other than structural and implementation details. As soon as you grasp this fundamental idea that INTENT is of significance, then you will be on the right path.
Understand the role of intent in design patterns
Intent for Strategy. This is a Behavioral Pattern
Intent for Abstract Factory. This is a Creational Pattern
Strategy is a workaround for languages that don't have first-class functions. You pass in a strategy object that decides some policy that you want separated from the rest of the code. Think of sorting in Java and how they use Comparators, a Comparator is a strategy object that allows you to specify the policy for sorting separately from the sorting algorithm. That allows you to reuse the code by dropping in different strategies.
Abstract Factory is an object used to create other objects, with the abstract part being that you have a factory that returns an implementation of the factory, where users of the factory access it through an interface. So one factory implementation can be swapped out for another with no changes to users of the factories, because those users are depending on the objects' interfaces only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With