Hendra Kurniawan wrote:why do people keep saying that this...is better than this:
Well, assuming Junilu is right, and you're referring to using factory methods - although a proper factory method would be:
SomeObject.createSomeObject();
there are several reasons; of which just a few are:
1. You can validate parameters
before passing them to your constructor, which means that it can concentrate on the business of creating the object (and, if necessary, throw an Exception before creating
anything).
2. You can give the method a nice, meaningful name.
3. You can return a
subclass of the object without having to let your clients know (information hiding).
4. For immutable classes, you can return a cached version of the object - ie, you don't necessarily have to return a
new one - You can find a good example of this practise in
Integer.valueOf(int).
If however, you're referring to an 'object Factory',
you should probably look at
this page.
Winston