views:

204

answers:

10

I was just reading a thread on SO that was discussing the merits of Singleton vs. Static Classes.

Some people mentioned that pattern X appeared to be more of a 'factory' rather than a Singleton 'pattern'.

What are the differences between a 'factory' and a 'design pattern'?

A: 

Factories and Singletons are some of the many design pattern.

A factory pattern can be implemented as a singleton pattern that produces objects. A factory could also be an instanced class, and therefore not a singleton. Likewise, a singleton can be a factory, but it can also be something else, like a global settings manager or event registry.

Max Shawabkeh
+8  A: 

A "factory" is a specific design pattern: http://en.wikipedia.org/wiki/Factory_method_pattern

Similarly "singleton" is also a design pattern: http://en.wikipedia.org/wiki/Singleton_pattern

Paolo
A: 

A Factory is a Design Pattern - not the other way around.

Mark Seemann
A: 

You got it slightly wrong. "Factory" is a pattern too and contrasts here with "Singleton".

Joey
A: 

'Factory' is a type of design pattern. You can see a couple examples of the Abstract Factory here or the Factory Method here based on the context

SwDevMan81
A: 

factory is a design pattern :-) as is Singleton. one could argue that singleton is kind of a factory. It creates an object when needed and uses a set chaching policy (which is always return the same object once it has been creaqted) but that's achedemic and would generally just be confusing in most debates about structure

Rune FS
A: 

A factory is a type of design pattern. Basically a factory returns a class dependant on the needs of the calling class. All classes returned by the factory should share the same interface so you can invoke the same public methods over them (although how each class implements the method may be different).

Here's a good link http://en.wikipedia.org/wiki/Factory_method_pattern

SOA Nerd
A: 

a 'factory' rather than a Singleton 'pattern'

Let me flesh that out and place the quotation marks correctly:

a 'factory pattern' rather than a 'singleton pattern'

Both are design patterns.

Guffa
A: 

What are the differences between a 'factory' and a 'design pattern'?

Factory IS a design pattern.

FractalizeR
A: 

Lots of answers, but none seem to actually differentiate between the two patterns well. Let me try and see if I can't confuse issue more.

A Singleton is a pattern that restricts your system to creating only one instance of a given class. The restriction is usually implemented by creating a Factory that will either create an instance of the class (if none already exist) or return the already-created instance on subsequent calls.

A factory is used to create singletons and in other situations. It can be used to replace "new" in many cases. One advantage is that you can write your factory to allow the type of object to be returned to be "Set". This way your testing framework can "set" a mock object instead of the real one--and the rest of your system will then use the mock object.

Another case might be to have the factory evaluate from parameters which type to return, or from data (perhaps XML). They are also used to implement Dependency Injection where the factory looks at what you need and builds chains of objects to fulfill those needs.

Bill K