Factory Design Pattern

31 Jan 2013

The idea of this design pattern is to encapsulate object creation; to separate object creation from the interface. Factory is basically a class where all kinds of object creating activities actually happen.

When you need to create an object instance, you simply delegate this task to Factory, and use the reference to Factory object to call some function in Factory to create objects you need.

To see some Factory pattern in the wild, see how GEOS library create Geometry objects:

/** in src/geom/Geometry.cpp **/

Geometry::Geometry(const GeometryFactory *newFactory)
	:
	envelope(NULL),
	factory(newFactory),
	userData(NULL)
{
	if ( factory == NULL ) {
		factory = GeometryFactory::getDefaultInstance();
	} 
	SRID=factory->getSRID();
}

And the GeometryFactory class is defined in src/geom/GeometryFactory.cpp where all sorts of createXXX() methods are defined. Go checkout the source code yourself.