Thursday, 26 July 2012

Factory Pattern


What is Factory Pattern?

Factory Method pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.

Explanation

This pattern is very simple.The client needs a product, but instead of creating it directly using the new operator, it asks the factory object for a new product, providing the information about the type of object it needs.
The factory instantiates a new concrete product and then returns to the client the newly created product(casted to abstract product class).The client uses the products as abstract products without being aware about their concrete implementation.


This adheres the OCP of SOLID design principles.

ADO.Net uses the factory method pattern for creating connection object based on providers.

Conclusion

While designing an application just think if you really need it a factory to create objects.Using it unnecessarily may bring unnecessary complexity in application. If you have many objects of the same base type and you manipulate them mostly casted to abstract types, then you need a factory. 

No comments:

Post a Comment