Questions tagged [abstract-factory]

Abstract Factory is a creational design pattern published by the Gang of Four. Its intent is to, "Provide an interface for creating families of related or dependent objects without specifying their concrete classes." (page 87) Note that [factory-method] is a separate GoF pattern, and there are additional [factory] patterns outside the GoF.

The Abstract Factory Pattern enforces a theme across a group of related objects. The canonical example from the GoF book is a look-and-feel standard enforced across a group of GUI widgets. It is implemented by five participants. (page 89)

  • AbstractFactory (e.g. WidgetFactory) - declares an interface for operations that create AbstractProduct objects.
  • ConcreteFactory (e.g. MotifWidgetFactory, PMWidgetFactory) - implements the operations to create ConcreteProduct objects.
  • AbstractProduct (e.g. Window, ScrollBar) - declares an interface for a type of product object.
  • ConcreteProduct (e.g. MotifWindow, MotifScrollBar) - defines a product object to be created by the corresponding ConcreteFactory and implements the AbstractProduct interface.
  • Client - uses only interfaces declared by AbstractFactory and AbstractProduct classes.

The Gang of Four offer the following criteria for applying the Abstract Factory Pattern. (page 88)

Use the Abstract Factory pattern when

  • a system should be independent of how its products are created, composed, and represented.
  • a system should be configured with one of multiple families of products.
  • a family of related product objects is designed to be used together, and you need to enforce this constraint.
  • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

The pattern has the following consequences. (page 89)

  • It isolates concrete classes.
  • It makes exchanging product families easy.
  • It promotes consistency among products.
  • Supporting new kinds of products is difficult.

See Abstract Factory for a longer summary of the GoF book.

Note the Gang of Four published two different factory patterns, the other being . Additionally, there are factory patterns outside the GoF book, so the term "factory" by itself is ambiguous.

252 questions
604
votes
21 answers

What are the differences between Abstract Factory and Factory design patterns?

I know there are many posts out there about the differences between these two patterns, but there are a few things that I cannot find. From what I have been reading, I see that the factory method pattern allows you to define how to create a single…
524
votes
19 answers

What is the difference in case of intent and application between these two Patterns?

Factory and Abstract Factory are both creational patterns. What is the difference in case of intent and application between these two Patterns?
user366312
  • 16,949
  • 65
  • 235
  • 452
220
votes
15 answers

What is the difference between Factory and Strategy patterns?

Can any one explain the difference between factory and strategy patterns? For me both are looking same other than an extra factory class (which create an object of product in factory patterns)
146
votes
10 answers

Design Patterns: Abstract Factory vs Factory Method

Note: Questions are at the end of the post. I have read the other stackoverflow threads regarding Abstract Factory vs Factory Method. I understand the intent of each pattern. However, I am not clear on the definition. Factory Method defines an…
140
votes
15 answers

Why do we need Abstract factory design pattern?

Most of the definition says: An abstract factory provides an interface for creating families of related objects without specifying their concrete classes What is the use of Abstract Factory Pattern as we can achieve the task via creating…
Amit
  • 3,358
  • 9
  • 34
  • 48
47
votes
1 answer

Abstract factory pattern

Good example for Abstract factory pattern in C#? What are the advantages of the Abstract factory pattern in C#? How to use C# generics with the Abstract factory pattern? How to unit test with the Abstract factory pattern?
45
votes
2 answers

What are the differences between facade pattern and abstract factory pattern?

I'm not asking the interview point of view. What is the real time scenario of implementing it in the projects, like the Struts framework, etc.?
44
votes
3 answers

Avoiding all DI antipatterns for types requiring asynchronous initialization

I have a type Connections that requires asynchronous initialization. An instance of this type is consumed by several other types (e.g., Storage), each of which also require asynchronous initialization (static, not per-instance, and these…
43
votes
5 answers

Is the Service Locator pattern any different from the Abstract Factory pattern?

At first glance, the Service Locator pattern looks the same as the Abstract Factory pattern to me. They both seem to have the same use (you query them to receive instances of abstract services), and they both have been mentioned when I read about…
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
43
votes
3 answers

Factory, Abstract Factory and Factory Method

I am really confused about these three terms. My understanding is that: in the Factory pattern, there is no concrete factory. The factory builds the new objects according to the parameters. in Abstract Factory pattern, there are multiple concrete…
skydoor
  • 25,218
  • 52
  • 147
  • 201
34
votes
6 answers

Abstract Factory, Factory Method, Builder

It may seem as if this is question is a dupe, but please bear with me - I promise I've read the related posts (and the GOF book). After everything I've read, I still don't have it clear when to use an Abstract Factory, a Factory Method, or a…
14
votes
1 answer

Dependency Injection with Ninject, MVC 3 and using the Service Locator Pattern

Something that has been bugging me since I read an answer on another stackoverflow question (the precise one eludes me now) where a user stated something like "If you're calling the Service Locator, you're doing it wrong." It was someone with a high…
12
votes
3 answers

AbstractFactory Versus Bridge Pattern

I have just learned the Bridge Pattern and its intent : Decouple an abstraction from its implementation so that the two can vary independently. But why couldn't just an AbstractFactory do the same thing ? I know that an AbstractFactory can create a…
Mik378
  • 21,881
  • 15
  • 82
  • 180
12
votes
1 answer

Working with Abstract Factory that is injected through DI container

I`m confused about Dependency Injection implementation in one concrete example. Let's say we have a SomeClass class that has a dependency of type IClassX. public class SomeClass { public SomeClass(IClassX dependency){...} } Creation of…
22db05
  • 675
  • 1
  • 9
  • 13
11
votes
4 answers

Why does Abstract Factory use abstract class instead of interface?

I am learning about design patterns and the first example in the book is about Abstract Factory. I have built the exercise in VS and all looks good, but there is one question that I wonder about. In the book the factory class is implemented like…
user1615362
  • 3,657
  • 9
  • 29
  • 46
1
2 3
16 17