Definition:
A factory
design pattern can be defined as "A factory is an object for creating the
other objects
General
Understanding:
In technical
terms, we can say that a factory is a class with a method. That method will create
and return different types of objects based on the input parameter, it
received.
we create an object without exposing the creation logic to
the client and the client will refer to the newly created object using a common
interface.
Diagram:
Program:
Create a class with the name
Creational_Factory_DesignPattern.cs
. Copy paste the below code and run that's it. I have even included comments for
your reference.
Creational_Factory_DesignPattern.cs
/*Program Start*/
using System;
namespace DesignPatterns
{
//Step1:
Create the Product Interface
public interface ICreditCard
{
string GetCardType();
int GetCreditLimit();
int GetAnnualCharge();
}
//Step2:
Creating Product classes MoneyBack,Titanium,Platinum
class MoneyBack : ICreditCard
{
public string GetCardType()
{
return "MoneyBack";
}
public int GetCreditLimit()
{
return 15000;
}
public int GetAnnualCharge()
{
return 500;
}
}
public class Titanium : ICreditCard
{
public string GetCardType()
{
return "Titanium Edge";
}
public int GetCreditLimit()
{
return 25000;
}
public int GetAnnualCharge()
{
return 1500;
}
}
public class Platinum : ICreditCard
{
public string GetCardType()
{
return "Platinum Plus";
}
public int GetCreditLimit()
{
return 35000;
}
public int GetAnnualCharge()
{
return 2000;
}
}
/*Solution
Block Start*/
class CreditCardFactoryPattern
{
public static ICreditCard
GetCreditCard(string cardType)
{
ICreditCard cardDetails = null;
if (cardType == "MoneyBack")
{
cardDetails = new MoneyBack();
}
else if (cardType == "Titanium")
{
cardDetails = new Titanium();
}
else if (cardType == "Platinum")
{
cardDetails = new Platinum();
}
return cardDetails;
}
}
/*Solution
Block End*/
//Step3:
Client Code
class FactoryPattern
{
static void Main(string[] args)
{
/*Problem - Usual Program execution */
//Generally we will get the Card Type from
UI.
//Here we are hardcoded the card type
string cardType = "MoneyBack";
ICreditCard cardDetails = null;
//Based of the CreditCard Type we are
creating the
//appropriate type instance using if else
condition
if (cardType == "MoneyBack")
{
cardDetails = new MoneyBack();
}
else if (cardType == "Titanium")
{
cardDetails = new Titanium();
}
else if (cardType == "Platinum")
{
cardDetails = new Platinum();
}
if (cardDetails != null)
{
Console.WriteLine("CardType :
" +
cardDetails.GetCardType());
Console.WriteLine("CreditLimit :
" +
cardDetails.GetCreditLimit());
Console.WriteLine("AnnualCharge
:" +
cardDetails.GetAnnualCharge());
}
else
{
Console.Write("Invalid Card
Type");
}
/*Problem End here*/
/*Solution - Implementing Factory pattern*/
ICreditCard cardDetailsSolution =
CreditCardFactoryPattern.GetCreditCard("Platinum");
if (cardDetails != null)
{
Console.WriteLine("CardType :
" +
cardDetailsSolution.GetCardType());
Console.WriteLine("CreditLimit :
" +
cardDetailsSolution.GetCreditLimit());
Console.WriteLine("AnnualCharge
:" +
cardDetailsSolution.GetAnnualCharge());
}
else
{
Console.Write("Invalid Card
Type");
}
/* Solution End Here*/
Console.ReadLine();
}
}
}
/*Program End*/
Interesting
Questions:
1.What is
Factory Design Pattern?
A factory design pattern can be defined as "A factory is
an object for creating the other objects
2.Understanding
the Factory Design Pattern.
·
In simple words, if we have a superclass and n
number of subclasses, and based on the data provided, if we have to create and
return the object of one of the subclasses, then we need to use the factory
design pattern.
·
The basic principle behind the factory design
pattern is that, at run time, we get an object of similar type based on the
parameter we pass.
3.Example
without using the Factory Pattern.
Refer the above program
4.Understanding
the Problem of not using the factory design pattern
·
First, the tight
coupling between the client class (Program) and Product Class (MoneyBack,
Titanium, and Platinum).
·
Secondly, if we add
a new Credit Card, then also we need to modify the Main method by adding an
extra if-else condition which not only overheads in the development but also in
the testing process
5.Implementing
the Factory Design Pattern in C#?
Create a FactoryClass and cook the objects inside the class
as I have created
"CreditCardFactoryPattern" and return
the required object based on the input passed in the main method.
6.Real
-Life Example of Factory Pattern:
·
From Lehman’s point
of view, we can say that a factory is a place where products are created. In
order words, we can say that it is a centralized place for creating products.
Later, based on the order, it received, the appropriate product is delivered by
the factory.
·
For example, a car
factory can produce different types of cars. If you are ordering a car to the
car factory, then based on your requirements or specifications, the factory
will create the appropriate car and then delivered that car to you.
7.When to
use Factory Design Pattern in Real-time Application?
It would
not be a good programming approach to specify the exact class name while
creating the objects by the client which leads to tight coupling between the
client and the product. To overcome this problem, we need to use the Factory
Design Pattern in C#.
This design
pattern provides the client with a simple mechanism to create the object. So,
we need to use the Factory Design Pattern in C# when
1. The Object
needs to be extended to the subclasses
2. Classes
don’t know what exact sub-classes it has to create
3. The Product
implementation going to change over time and the Client remains unchanged
Problems of Simple Factory Pattern in C#
·
If we need to add any new product (i.e. new credit
card) then we need to add a new if else condition in the GetCreditCard method
of the CreditCardFactory class. This violates the open/closed design principle.
·
We also have a tight coupling between the Factory
(CreditCardFactory) class and product classes (MoneyBack, Titanium, and
Platinum).
In the next
article, I am going to discuss how to overcome the above problem by using
the Factory Method Design Pattern in
C#. Here, in this article, I try to explain the Factory Pattern in
C# with an example. I hope this article will help you with your needs. I
would like to have your feedback. Please post your feedback, question, or comments
about this article.
No comments:
Post a Comment