Simple Factory Design Pattern With
RIP
1
Arifur Rahman
Software Engineer
Brain Station-23
What is factory pattern ?
The factory pattern is a popularly used design pattern and it is very useful to restrict clients
from knowing the actual business logic methods, it is useful to create a decoupled system,
and it is useful to eliminate object creation in a client environment.
Intent :
1. Creates objects without exposing the instantiation logic to the client.
2. Refers to the newly created object through a common interface.
2
Problem :
For example we have a scenario where based on user input we need to call particular class methods.
//
// customer enters their choice
//
If (choice == "Car")
{
// call car class and it’s methods
Car c = new Car();
c.buy();
}
If (choice == "bike")
{
// call bike class and it’s methods
Bike b = new Bike();
b.buy();
}
3
Factory pattern
public enum ChoiceType : int
{
Bike = 1,
Car
}
// Interface
public interface IChoice
{
string Buy();
}
public class clsBike : IChoice
{
public string Buy(){
return ("You choose Bike");
}
}
public class clsCar : IChoice
{
public string Buy(){
return ("You choose Car");
}
}
4
Factory pattern contd.
// Factory Class
public class FactoryChoice
{
static public IChoice getChoiceObj(ChoiceType choice)
{
IChoice objChoice = null;
if (choice == ChoiceType.Car)
{
objChoice = new clsCar();
}
else if (choice == ChoiceType.Bike)
{
objChoice = new clsBike();
}
return objChoice;
}
}
5
RIP (Replace IF with Polymorphism)
public class FactoryChoice
{
private IDictionary<ChoiceType, IChoice> _choices;
public FactoryChoice()
{
_choices = new Dictionary<ChoiceType, IChoice>
{
{ChoiceType.Bike, new clsBike() },
{ChoiceType.Car, new clsCar()}
};
}
static public IChoice getChoiceObj(ChoiceType choice)
{
var factory = new FactoryChoice();
return factory._choices[choice];
}
}
6
Factory pattern contd.
class Program
{
static void Main(string[] args)
{
//Client class
IChoice objInvoice;
objInvoice = FactoryChoice.getChoiceObj(ChoiceType.Bike);
Console.Write(objInvoice.Buy());
Console.ReadKey();
}
}
7
8

Factory pattern with rip

  • 1.
    Simple Factory DesignPattern With RIP 1 Arifur Rahman Software Engineer Brain Station-23
  • 2.
    What is factorypattern ? The factory pattern is a popularly used design pattern and it is very useful to restrict clients from knowing the actual business logic methods, it is useful to create a decoupled system, and it is useful to eliminate object creation in a client environment. Intent : 1. Creates objects without exposing the instantiation logic to the client. 2. Refers to the newly created object through a common interface. 2
  • 3.
    Problem : For examplewe have a scenario where based on user input we need to call particular class methods. // // customer enters their choice // If (choice == "Car") { // call car class and it’s methods Car c = new Car(); c.buy(); } If (choice == "bike") { // call bike class and it’s methods Bike b = new Bike(); b.buy(); } 3
  • 4.
    Factory pattern public enumChoiceType : int { Bike = 1, Car } // Interface public interface IChoice { string Buy(); } public class clsBike : IChoice { public string Buy(){ return ("You choose Bike"); } } public class clsCar : IChoice { public string Buy(){ return ("You choose Car"); } } 4
  • 5.
    Factory pattern contd. //Factory Class public class FactoryChoice { static public IChoice getChoiceObj(ChoiceType choice) { IChoice objChoice = null; if (choice == ChoiceType.Car) { objChoice = new clsCar(); } else if (choice == ChoiceType.Bike) { objChoice = new clsBike(); } return objChoice; } } 5
  • 6.
    RIP (Replace IFwith Polymorphism) public class FactoryChoice { private IDictionary<ChoiceType, IChoice> _choices; public FactoryChoice() { _choices = new Dictionary<ChoiceType, IChoice> { {ChoiceType.Bike, new clsBike() }, {ChoiceType.Car, new clsCar()} }; } static public IChoice getChoiceObj(ChoiceType choice) { var factory = new FactoryChoice(); return factory._choices[choice]; } } 6
  • 7.
    Factory pattern contd. classProgram { static void Main(string[] args) { //Client class IChoice objInvoice; objInvoice = FactoryChoice.getChoiceObj(ChoiceType.Bike); Console.Write(objInvoice.Buy()); Console.ReadKey(); } } 7
  • 8.