Monday, May 5, 2014

Factory Method Pattern

Factory method pattern is  a type of Creational category of Design pattern. Factory Method Pattern defines an interface for creating objects and it assigns the privilege for creating objects to sub classes. It abstracts the logic for creating which type of class to create.

  • Objects created through Factory method avoids the dependency of Sub classes code with specific implementation of the interface which it implements.
  • Factory method returns the same object for same set of parameters and hence we can skip duplicate objects
  • We can create and return any sub type of the type that factory is designed to create.
  • Object creation is abstract from client code


Below diagram explains the pattern in high level with different components involved.


Lets explain the above components in brief as follows

Product: Defines the interface for objects
Concrete Product: Implements the Product interface
Creator : Declares the factory method which returns the Product
Concrete Creator:Overrides the factory method to return Concrete Product.

Lets have the following implementation for the Factory method.

Product :



Concrete Product :

Creator : 

Implementation :

So here we have defined the Factory Method. Keep watching this space for more articles on Design Patterns.





3 comments:

Installing ASP.NET MVC

Installing ASP.NET MVC Before we dive deep into the ASP.NET MVC, let’s install it our development machine. This is very straight forwa...