Factory Method Design Pattern
A factory Method design pattern is well known creational pattern. It provide interface to create object, but let sub class decide whose object will be created. In other words creating object without exposing the instantiation logic to client.
Factory Method design pattern real time example:
In real time example bank has many account type like Saving, Current and Fixed accounts. All account type has different type of facilities. Here we have account factory where facilities are defined as per there type.
UML Class Diagram
The participant’s classes in factory method patterns are:
1. Product: Interface for objects the factory method creates. In our example
Facilities is product.
2. ConcreteProduct: Implementation of product interface. All the concrete
products are sub class of Product class. Interest, OverDue, Deposit, Withdrawal,
Cheque and ATM is concreteproduct.
3. Creator: It is also called factory because it creates the product object.
It declares the factory method which returns the object. Account is creator in our
example.
4. ConcreteCreator: Override the generating method for creating concreteproduct
object. It is responsible for creating concreteproduct object. In our example Saving,
Current and Fixed is ConcreteCreator.
Class Implementation
namespace
designpattern
{
public
abstract
class
Facilities
{
public
abstract
void
GetData();
}
public class
Interest
: Facilities
{
public
override
void
GetData()
{
Console.Write("Interest
Given");
}
}
public class
OverDue
: Facilities
{
public
override
void
GetData()
{
Console.Write("Overdraft
Given");
}
}
public class
Deposit
: Facilities
{
public
override
void
GetData()
{
Console.Write("Deposit");
}
}
public class
Widthdrawal : Facilities
{
public
override
void
GetData()
{
Console.Write("Widthdrawal");
}
}
public class
Cheque
: Facilities
{
public
override
void
GetData()
{
Console.Write("Cheque");
}
}
public class
ATM
: Facilities
{
public
override
void
GetData()
{
Console.Write("ATM");
}
}
public abstract
class
Account
{
List
public
List
{
}
public
Account()
{
GetFacilites();
}
public
abstract
void
GetFacilites();
}
public class
Saving
: Account
{
public
override
void
GetFacilites()
{
AccountFacilities.Add(new
Interest());
AccountFacilities.Add(new
Deposit());
AccountFacilities.Add(new
Widthdrawal());
AccountFacilities.Add(new
Cheque());
AccountFacilities.Add(new
ATM());
}
}
public class
Current
: Account
{
public
override
void
GetFacilites()
{
AccountFacilities.Add(new
Deposit());
AccountFacilities.Add(new
Widthdrawal());
AccountFacilities.Add(new
OverDue());
AccountFacilities.Add(new
Cheque());
}
}
public class
Fixed
: Account
{
public
override
void
GetFacilites()
{
AccountFacilities.Add(new
Interest());
AccountFacilities.Add(new
Deposit());
AccountFacilities.Add(new
Widthdrawal());
}
}
}
Pro and Cons
Pros:
1. It implement loose coupling in the
application. It provides simple way of extending the family of products with minor
change in application.
2. Existing object can easily modify
or replace without changing existing code.
Cons:
1. Classes not implement with base class cannot use in factory implementation.
| Sample Code |
| Date:- 15-March, 2010 |
|
Reference http://www.dofactory.com/Patterns/PatternFactory.aspx |