Wikipedia has a factory pattern page.
For simple cases there really is no trick. Just
Foo* createFoo() {
return new Foo();
}
It gets trickier when you want to do more than just use new. One good example is if the constructor takes a number of parameters, or if the objects need to be initialized somehow. In that case you can load up the factory with the requirements and not make the developer worry about them:
class BarFactory {
BarFactory(Dep* x, Depen* y) ...
getBar() {
return new Bar(x->SOME_METHODS, y->SOMETHINGELSE, ...);
}
}
In that example the the factory takes the confusion out of correctly making a Bar object, (imagine it took more arguments and they needed a lot of hand holding). This can be helpful when you've got an API with a lot of options that don't change or just a bad API.