Announcement
iPhone DZone: Fresh Links On Your iPhone by matt at Sat Jul 05 14:09:51 EDT 2008
Reading DZone on your iPhone just got a little bit easier. Visit http://dzone.com/iphone from your iPhone to try out our beta iPhone support. You can view stories, filter by tags, and login. We'd appreciate your feedback at feedback@dzone.com.
Vote early, vote often! When you help pick the links, you'll like DZone better. Login and vote now.
By bloid
via reiersol.com
Published: May 06 2008 / 07:32
Everybody who writes object-oriented code knows about constructors. You need them so the program knows how to instantiate objects, right? And you especially need them when a lot of things have to be done while instantiating an object. And personally, I've never considered visibility restrictions important enough to be a major argument against those languages that have lacked them (PHP 4). So why would I be skeptical of public constructors?
Comments
jakyra replied ago:
An interesting way to get around not being able to overload functions, at least at the constructor level.
antych replied ago:
I would consider those factory methods to be a supplement for a standard model, use them where appropriate. Limiting access to any method, not just constructor without a good reason makes your code less testable. btw. you can use "new self()" inside that static call.
nwbrown replied ago:
Sure, there are plenty of times where factory methods are better than public constructors. But to make the leap from that to "{0} considered harmful" is a bit of a stretch.
isnoop replied ago:
Useful? Perhaps...
Harmful? Hardly.
No need for the hysterics.
paul_houle replied ago:
A good blog post with a poor title. There are many cases where static "create" methods or Factory objects make sense.
Voters For This Link (6)
Voters Against This Link (12)