Basically a factory method is a method which returns an object (just like a factory manufactures goods -a factory method manufactures an object -and that’s why the name). To understand it with an example, lets assume you have an interface, very creatively called MyInterface, which you want to implement. So, you implement it in a class called, say, FirstImplementation. And then you make use of FirstImplementation in, say, 120 odd other classes in your software. All ok so far!… But the problem will arise if you need to use a different implementation of MyInterface. Your options would be: Well, the first option is feasible. But in that case you will lose the code of the FirstImplementation. This is not a very neat way to solve the problem. Second option is simply nightmarish! Only programmers as diligent as computers will attempt it as you will have to change 120 odd classes. Third option is beautiful, neat and smart. You just create a new intermediate class, say FactoryClass, and use this class to get the instance of the FirstImplementation in your 120 odd classes. Now, if you would need to use a different implementation of MyInterface -all you need to do is just to modify the code in the FactoryClass. So now if you decide to implement MyInterface as SecondImplementation -all you need to do is to change the code in the FactoryClass. You don’t need to touch the UserClass(es) for this. Did you get the idea? If you’re still in doubt about the concept of factory methods, feel free to comment and ask me. I will try to help you. Comment * Name * Email * Website

Δ