Thursday, November 1, 2007

Example of the Factory Method Pattern

After reading about the factory method pattern, nothing that I have done in the past really jumped out at me so I decided to look through the Java 1.5 API and found an example of a class that follows the factory method pattern. This class wasn't to hard to find since the name has "Factory" in it, ObjectFactory. The actual method in the ObjectFactory class that follows the factory method pattern is getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment), which returns an object. The point of the factory method pattern is to create objects without specifying the exact class of object that will be created. This method is used to allow subclasses to choose which type of object to create.

1 comment:

Dondi said...

OK, that works, but just so you know, BorderFactory was also just under your nose and you've probably used it before (unlike ObjectFactory). Also, even though "factory" isn't in its name, the Box class also has factory methods in it (the create*() stuff).

But cool, that works, move along... :)