Using Factory Method Pattern vs Constructor pattern

Currently, we've been using constructors to create objects with new 
Object pattern. I think we should move to a Factory Method Pattern for 
object construction.

The pros:
- works better for abstract interfaces so what internal object (even 
derived) can be created independent of object
- allows more meaningful method signatures, e.g. createWithFooBar(...) 
or listen(...)
- allows easier extensions. There's only so many ways to rearrange 
parameter orders to get unique method signatures, whereas we can always 
extend factory method names
- allows easier creation of singleton / static helper objects used with 
other patterns
- better encapsulation

The cons:
- "new" knowingly creates a new instance
- consistency with other APIs?
- more methods exposed in the interface (but not really since 
constructor has as well, it's just more implicit)

Thoughts?

-Robin

Received on Friday, 11 April 2014 18:13:44 UTC