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.

223 questions
730
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 ?
ALEXALEXIYEV
  • 14,924
  • 40
  • 119
  • 187
504
votes
19 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…
500
votes
19 answers

What is the basic difference between the Factory and Abstract Factory Design Patterns?

What is the basic difference between the Factory and Abstract Factory Patterns?
user366312
  • 17,582
  • 55
  • 198
  • 392
348
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
  • 64,918
  • 23
  • 156
  • 223
291
votes
15 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
279
votes
14 answers

What are static factory methods?

What's a "static factory" method?
freddiefujiwara
  • 49,933
  • 28
  • 71
  • 102
205
votes
8 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…
144
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…
35
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
  • 56,707
  • 23
  • 140
  • 187
33
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…
27
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,483
  • 1
  • 12
  • 19
26
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,142
  • 2
  • 11
  • 22
19
votes
10 answers

What Are Some Examples of Design Pattern Implementations Using JavaScript?

I'm a moderately skilled programmer using JavaScript but I am no guru. I know you can do some pretty powerful things with it, I just haven't seen much other than fairly basic DOM manipulation. I'm wondering if people could provide some examples of…
Aaron
  • 22,132
  • 10
  • 45
  • 48
15
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
  • 11,896
  • 36
  • 120
  • 185
14
votes
1 answer

Autowiring of beans generated by EasyMock factory-method?

I have a problem that seems really strange to me. I have the following setup: An interface: package com.example; public interface SomeDependency { } A spring component: package com.example; @Component public class SomeClass { } A spring test…
matsev
  • 26,664
  • 10
  • 102
  • 138
1
2 3
14 15