Questions tagged [factory-method]

Factory Method is a creational design pattern published by the Gang of Four. Its intent is to, "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses." (page 107) Note that [abstract-factory] is a separate GoF pattern, and there are additional [factory] patterns outside the GoF.

The Factory Method Pattern represents a virtual constructor. The canonical example from the GoF book is a framework for applications that present multiple documents. The framework handles document creation, but the type of document varies for each concrete application using the framework. In other words, the framework, (page 107)

...only knows when a new document should be created, not what kind of Document to create. This creates a dilemma: The framework must instantiate classes, but it only knows about abstract classes, which it cannot instantiate.

The Factory Method pattern offers a solution. It encapsulates the knowledge of which Document subclass to create and moves this knowledge out of the framework.

The pattern is implemented by four participants. (page 108)

  • Product (Document) - defines the interface of objects the factory method creates.
  • ConcreteProduct (MyDocument) - implements the Product interface.
  • Creator (Application) - declares the factory method, which returns an object of type Product.
  • ConcreteCreator (MyApplication) - overrides the factory method to return an instance of a ConcreteProduct.

The Gang of Four offer the following criteria for applying the Factory Method Pattern.

Use the Factory Method pattern when

  • a class can't anticipate the class of objects it must create.
  • a class wants its subclasses to specify the objects it creates.
  • classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

The pattern has several variants. (page 110)

  • The two main variations of the Factory Method pattern are (1) the case when the Creator class is an abstract class and does not provide an implementation for the factory method it declares, and (2) the case when the Creator is a concrete class and provides a default implementation for the factory method.
  • Another variation on the pattern lets the factory method create multiple kinds of products. The factory method takes a parameter that identifies the kind of object to create. All objects the factory method creates will share the Product interface.

See Factory Method 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.

251 questions
822
votes
27 answers

What is the difference between Builder Design pattern and Factory Design pattern?

What is the difference between the Builder design pattern and the Factory design pattern? Which one is more advantageous and why ? How do I represent my findings as a graph if I want to test and compare/contrast these patterns ?
Penguen
  • 16,836
  • 42
  • 130
  • 205
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
384
votes
11 answers

How to implement the factory method pattern in C++ correctly

There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple: How do I implement Factory Method in C++ correctly? Goal: to make it possible…
Kos
  • 70,399
  • 25
  • 169
  • 233
335
votes
17 answers

Factory Pattern. When to use factory methods?

When is it a good idea to use factory methods within an object instead of a Factory class?
jjshell
305
votes
14 answers

What are static factory methods?

What's a "static factory" method?
freddiefujiwara
  • 57,041
  • 28
  • 76
  • 106
252
votes
9 answers

Design Patterns: Factory vs Factory method vs Abstract Factory

I was reading design patterns from a website There I read about Factory, Factory method and Abstract factory but they are so confusing, am not clear on the definition. According to definitions Factory - Creates objects without exposing the…
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…
39
votes
3 answers

Factory Pattern - CreateInstance static or not?

This is about the Factory Pattern. I am a little confused. I saw implementations where the createInstance() method is static and some implementations that are non-static. Some say it's depending on "style" or "taste" and some say it does not.…
dknaack
  • 60,192
  • 27
  • 155
  • 202
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…
30
votes
1 answer

Factory method for objects - best practice?

This is a question regarding the best practice for creating an instance of a class or type from different forms of the same data using python. Is it better to use a class method or is it better to use a separate function altogether? Let's say I have…
Yani
  • 1,465
  • 2
  • 16
  • 25
29
votes
4 answers

Is it OK for a factory method to return null?

I'm wondering about best practice here. Is it good practice for a factory method to return null if it can't create anything? Here's an example: ICommand command = CommandFactory.CreateCommand(args); if (command != null) command.Execute(); else …
Jeff Pratt
  • 1,619
  • 1
  • 13
  • 21
22
votes
1 answer

Dart factory (constructor) vs. static method; e.g., why is int.parse() not a factory constructor?

The Dart Style Guide recommends using constructors instead of static methods to create instances, given that "named constructors and factory constructors in Dart give you all of the flexibility of static methods in other languages, while still…
Patrice Chalin
  • 15,440
  • 7
  • 33
  • 44
20
votes
5 answers

How to preserve order of insertion in Map.of factory?

Java 9 offers Map.of() feature to easily create a map with fixed values. Problem: I want to create a map that preserves order of insertion like LinkedHashMap. Is that possible with that factory? At least map.of() does not preserv the order...
membersound
  • 81,582
  • 193
  • 585
  • 1,120
17
votes
6 answers

Is a switch statement applicable in a factory method? c#

I want to return an Interface and inside a switch statement I would like to set it. Is this a bad design? private IResultEntity GetEntity(char? someType) { IResultEntity entity = null; switch (someType) { …
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
1
2 3
16 17