posted 20 years ago
Hi Engin
This is incorrect. A factory is not used to prevent having too many instances of a class. There is a pattern that ensures that you have no more than one instance of a class--that pattern is called singleton.
A factory is used as an interface for object creation that allows subclasses to decide which object to instantiate. It is typically used when it cannot be anticipated what specific class needs to be created, or when you want to delegate responsibility of object creation to a helper.
The advantage of this is that the client code only has to deal with the interface, and not rely on some specific implementation. The design can then be more open.
A typical example of a factory is a when a container hands out datasource connections.
Eben Hewitt. SCJP, SCWCD, SCJD, SCJWSD for JEE 5, TOGAF 8 Certified Architect, author of Java SOA Cookbook (O'Reilly, 2009) and contributor to 97 Things Every Software Architect Should Know