How to implement factory design pattern?


Are you new to factory design pattern? Factory design pattern hides complexity from end user. I hope this code will be helpful for beginners of .Net design patterns. Factory is a object oriented creational pattern.

How to implement factory design pattern?
Create windows application for factory pattern demonstration.
You have to create one interface which will contain one method. Lets create a class file "IChooseVehicle.cs" and put following code into it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryWindowsFormsApplication
{
interface IChooseVehicle
{
void GetVehicleDetails();
}
}

Now we will implement GetVehicleDetails() in three different classes and will make enum of it.
Create one folder "VehicleTypes" and create class files "Bus.cs" , "Train.cs" and "Ship.cs" in it. Put following code in these class files as given below.
Code in Bus.cs is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FactoryWindowsFormsApplication.VehicleTypes
{
class Bus :IChooseVehicle
{
public void GetVehicleDetails()
{
MessageBox.Show("You have selected bus for transportation." ,"Transportation");
}
}
}

Code in Ship.cs is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FactoryWindowsFormsApplication.VehicleTypes
{
class Ship :IChooseVehicle
{
public void GetVehicleDetails()
{
MessageBox.Show("You have selected ship for transportation.", "Transportation");
}
}
}

Code in Train.cs is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FactoryWindowsFormsApplication.VehicleTypes
{
class Train : IChooseVehicle
{
public void GetVehicleDetails()
{
MessageBox.Show("You have selected train for transportation.", "Transportation");
}
}
}


Code in "EnumClass.cs" is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryWindowsFormsApplication
{
enum VehicleType
{
Bus = 0,
Ship = 1,
Train = 2
}
}

Create class file "VehicleFactoryPattern.cs" which will contain factory design pattern implementation.
Code of "VehicleFactoryPattern.cs" is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FactoryWindowsFormsApplication.VehicleTypes;

namespace FactoryWindowsFormsApplication
{
class VehicleFactoryPattern
{
private static VehicleFactoryPattern obj;
private VehicleFactoryPattern()
{ }
internal static VehicleFactoryPattern Singleton
{
get
{
if (obj == null)
{
obj = new VehicleFactoryPattern();
}

return obj;
}
}

internal IChooseVehicle GetVehicle(VehicleType vt)
{
IChooseVehicle selectedVechicle = null;

switch (vt)
{
case VehicleType.Bus:
selectedVechicle = new Bus();
break;

case VehicleType.Ship:
selectedVechicle = new Ship();
break;

case VehicleType.Train:
selectedVechicle = new Train();
break;
}

return selectedVechicle;
}
}
}

Code in Form1.cs is like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FactoryWindowsFormsApplication
{
public partial class frmVehicle : Form
{
public frmVehicle()
{
InitializeComponent();
}

private void frmVehicle_Load(object sender, EventArgs e)
{
cmbVehicle.Items.Add(VehicleType.Bus);
cmbVehicle.Items.Add(VehicleType.Ship);
cmbVehicle.Items.Add(VehicleType.Train);

}

private void btnOK_Click(object sender, EventArgs e)
{
VehicleType vt = (VehicleType)cmbVehicle.SelectedItem;
IChooseVehicle VehicleType = VehicleFactoryPattern.Singleton.GetVehicle(vt);
VehicleType.GetVehicleDetails();

}
}
}

When you execute the application, you have to select bus,train or ship by using combobox given on the form. As per the selection of input, method executes from respective classes.
Advantages of factory pattern
1) By using this, you can avoid making new keywords scattered.
2) Also you can hide how many vehicle types you have from end user,because you have to expose interface to end user and not the class files present in the Vehicle types folder.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: