Re: Differences between Singleton and Factory patterns
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.