praveen raaj wrote:
can you explain to me,what is difference between static factory method and constructor? and what are the advantages over constructor?
The difference is that a constructor is a
Java language element whereas a factory method is a design
pattern you apply. But both regards object construction. Each time an object is created a constructor of the class will be run.
One of the most common usages of a factory method is to increase encapsulation, like here,
The interface and the factory method are defined somewhere in a package. They're often the only things public in the package. The rest is implementation classes and stuff hidden within the package. So the only thing a user sees is the I interface and the create method. It's very easy to modify what concrete objects the create method returns. And the nice thing is the this doesn't change any code using I and create.
By doing this you've got a clean separation of a type (the I interface) and its implementations (the A, B objects).
The create method can be made more complex. For example by providing a parameter to create a user may be able to control which variation of I is to be returned (A and B could be slighty different in some respect).