Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the diffrence between strategy design pattern and abstract factory pattern?

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.

like image 871
RanZilber Avatar asked Feb 07 '11 20:02

RanZilber


People also ask

What is the difference between factory design pattern and Abstract Factory design pattern?

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.

What is the difference between strategy and template design pattern?

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.

What is difference between Abstract Factory and Factory Method design patterns Mcq?

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.

What is the difference between strategy and decorator pattern?

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.


2 Answers

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

  1. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
  2. Capture the abstraction in an interface, bury implementation details in derived classes.

Intent for Abstract Factory. This is a Creational Pattern

  1. Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  2. A hierarchy that encapsulates: many possible “platforms”, and the construction of a suite of “products”.
like image 85
Aravind Yarram Avatar answered Oct 14 '22 05:10

Aravind Yarram


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.

like image 25
Nathan Hughes Avatar answered Oct 14 '22 04:10

Nathan Hughes