Questions tagged [factory-pattern]

This tag is ambiguous. It may refer to one of the GoF patterns: Factory Method ([factory-method]) or Abstract Factory ([abstract-factory]). It may refer to patterns outside the GoF book. It may refer to all Factory ([factory]) patterns collectively, or it may refer to some other creational pattern. Please use another tag instead of this one.

This tag is ambiguous and alternatives should be used instead.

Consider one or more of the following.

  • is a pattern from the GoF book, based on inheritance.
  • is a pattern from the GoF book, based on composition.
  • is a pattern from Effective Java, based on static methods.
  • Simple Factory is a pattern from Head First Design Patterns, based on branching logic (if/else or switch/case).
  • is a category encompassing all of these patterns and more.

Also consider using the tag in addition to or instead of those listed above.

1470 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…
571
votes
30 answers

Dependency Injection vs Factory Pattern

Most of the examples quoted for usage of Dependency Injection, we can solve using the factory pattern as well. Looks like when it comes to usage/design the difference between dependency injection and factory is blurred or thin. Once someone told me…
Binoj Antony
  • 15,886
  • 25
  • 88
  • 96
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
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)
126
votes
11 answers

Why does Hibernate require no argument constructor?

The no-argument constructor is a requirement (tools like Hibernate use reflection on this constructor to instantiate objects). I got this hand-wavy answer but could somebody explain further? Thanks
unj2
  • 52,135
  • 87
  • 247
  • 375
107
votes
10 answers

How do I pass values to the constructor on my wcf service?

I would like to pass values into the constructor on the class that implements my service. However ServiceHost only lets me pass in the name of the type to create, not what arguments to pass to its contrstructor. I would like to be able to pass in…
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
88
votes
15 answers

Implement a simple factory pattern with Spring 3 annotations

I was wondering how I could implement the simple factory pattern with Spring 3 annotations. I saw in the documentation that you can create beans that call the factory class and run a factory method. I was wondering if this was possible using…
blong824
  • 3,920
  • 14
  • 54
  • 75
70
votes
1 answer

"Downcasting" unique_ptr to unique_ptr

I have a series of factories that return unique_ptr. Under the hood, though, they are providing pointers to various derived types, i.e unique_ptr, unique_ptr, unique_ptretc. Given DerivedA : Derived and Derived :…
d7samurai
  • 3,086
  • 2
  • 30
  • 43
64
votes
6 answers

Factory method with DI and IoC

I am familiar with these patterns but still don't know how to handle following situation: public class CarFactory { public CarFactory(Dep1,Dep2,Dep3,Dep4,Dep5,Dep6) { } public ICar CreateCar(type) { …
MistyK
  • 6,055
  • 2
  • 42
  • 76
51
votes
5 answers

instance factory methods Vs Static factory methods

Can't all factory methods be static ? Does something that produces a product need state ? When is it appropriate to go for a instance factory or static factory method ? Can you provide me examples differentiating the two ?
user2434
  • 6,339
  • 18
  • 63
  • 87
45
votes
7 answers

How to avoid 'instanceof' when implementing factory design pattern?

I am attempting to implement my first Factory Design Pattern, and I'm not sure how to avoid using instanceof when adding the factory-made objects to lists. This is what I'm trying to do: for (Blueprint bp : blueprints) { Vehicle v =…
Charles H
  • 625
  • 1
  • 7
  • 11
43
votes
20 answers

Is this Factory Method creation pattern?

I have been using factory method creation pattern for awhile now. I was just recently told that this: public static class ScheduleTypeFactory { public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType) { …
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
40
votes
5 answers

Real world examples of Factory Method pattern

I just read Factory Method. I understand that it provides a way to delegate the instantiation to sub-classes. But I couldn't understand the possible uses in a real-world scenario. Can anyone give one typical example showing how Factory method…
softwarematter
  • 28,015
  • 64
  • 169
  • 263
1
2 3
97 98