A day with .Net

My day to day experince in .net

Factory Method Design Pattern

Posted by vivekcek on September 13, 2012

DOWNLOAD http://sdrv.ms/OrSETl

definition by GOF:

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

Our Example

The participants classes in this pattern are:

IProduct: defines the interface for objects the factory method creates.

ConcreteProduct: implements the Product interface (ex: Book, Mobile).

IProductFactory: declares an interface with method CreateProduct,Implemented by ConcreteFactory.

ConcreteFactory: overrides the generating method for creating ConcreteProduct objects.(ex: BookFactory, MobileFactory)

All concrete products are implementation of the IProduct, so all of them have the same basic implementation,

In our example class Book and Mobile are concrete implementation of IProduct.

The classes BookFactory and MobileFactory implement IProductFactory. These classes are responsible for creating concrete objects of our Book and Mobile class according to client requirement.

In our example Program.cs is our client class. Client will tell which factory to use. Based on that we get instances of concrete products.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s