Singleton pattern over Factory pattern

Hi

What is the difference between the Factory and Singleton pattern.

Can we use a singleton (pattern) class to create other object instances and return them.

What are the advantages and disadvantages for using the Singleton pattern here.

Or do we have to use only a factory (pattern) class to do the above type of task.

Any input is highly appreciated.

Thanks.

[417 byte] By [ckollareddy] at [2008-2-1]
# 1

The Singleton and Factory patterns are fundamentally different. The factory pattern is more suitable for returning object instances. The caller is abstracted from the instantiation specifics of the object. On the other hand, the singleton pattern is used mostly when only one object instance needs to be exist at any point of time. Clients using a singleton would usually synchronize calls to the object to enforce single use. (Although it is now common practice to use the Singleton pattern to return more than instance, I don't belong to that club).

apdattani at 2007-7-2 > top of java,Other Topics,Patterns & OO Design...
# 2

What is the difference between the Factory and Singleton pattern.

These patterns are very different; they serve orthogonal requirements.

The purpose of the singleton pattern is to ensure that only one instance of a given class exists, the singleton its self.

The purpose of an [Abstract] Factory is to construct multiple objects of the same [polymorphic] type.

Can we use a singleton (pattern) class to create other object instances and return them.

Yes, an Abstract Factory could also be a singleton and in general this is a good idea, however it can cause complications on distributed clusters.

What are the advantages and disadvantages for using the Singleton pattern here.

The question does not really make sense in this context, it assumes a choice of Singleton or Factory which is not the case. The question only makes sense against a specific set of requirements. Then the advantage is they meet the requirement or they do not.

Or do we have to use only a factory (pattern) class to do the above type of task.

Perhaps Im missing something, but what task ?

MartinS. at 2007-7-2 > top of java,Other Topics,Patterns & OO Design...