Differences between Singleton and Factory patterns
2 posts in topic
Flat View  Flat View

Posted By:   Vijay_Kumar
Posted On:   Wednesday, March 17, 2004 12:26 AM

I would like to get to know about the differences between Singleton and Factory design patterns

Basically I would like to know the points to be considered to decide the type of pattern to be used and the situations where each pattern will be the best solution

Re: Differences between Singleton and Factory patterns

Posted By:   Iuga_Marin  
Posted On:   Monday, April 5, 2004 10:51 AM


The Singleton design pattern allows you to ensure that a class has only one instance, and to provide a single global point of access to this instance.

Suppose that you have some system-wide data that should be accessed from a single point. Think about the System class (even if isn't used as a singleton).
You'll use something like Singleton.getInstance() to access this unique instance and operate on it.

The Factory design pattern allows you to create different implementations of a specific interface without having to know directly the implementation classes. You'll use something like Factory.getInstance(some_info) to create implementations that meet your needs (as long as the factory knows how to create these implementations).

Look at java.security.cert.CertificateFactory class, especially at the method getInstance.

As you can see, you may create certificate of different types without having to know their implementation classes. All you have to do is to specify the type parameter when calling getInstance method.

Best regards,


Re: Differences between Singleton and Factory patterns

Posted By:   Christopher_Koenigsberg  
Posted On:   Sunday, March 21, 2004 08:51 PM

My understanding is that typically you will use a "static factory method", to provide access to a singleton. But there are other uses for factory methods; in particular I think the actual GOF book uses the static factory pattern for something different (hiding the specific implementation version).

About | Sitemap | Contact