Factory Design Pattern in C#
Golam Mostofa
??Unity 3D Developer |??Crafting Engaging Games |??Turning Ideas into Reality
This article explains how to implement Factory design pattern in C# and where it can be used.
What is this
The Factory Design Pattern is a commonly used design pattern where we need to create Loosely Coupled System. Basically, it comes under Creational Pattern and it is used to create instance and reuse it. Factory Method is a Design Pattern which defines an interface for creating an object, but lets the classes that implement the interface decide which class to instantiate. Factory Pattern lets a class postpone instantiation to sub-classes.
The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.
When to use Factory Design Pattern
It is used for creating objects to encapsulate the instantiation logic. Client doesn’t know the actual instantiation logic of entity.
What is Achieved by Using Factory Pattern?
- The object creation can be separated from the clients deciding on what objects to be created.
- Factory allows you to add new concrete classes and methods without breaking or modifying the existing code.
Factory Design in a diagram:
The classes and objects participating in the above UML class diagram are as follows:
Product
- This defines the interface of objects the factory method creates
ConcreteProduct
- This is a class which implements the Product interface.
Creator
- This is an abstract class and declares the factory method, which returns an object of type Product.
- This may also define a default implementation of the factory method that returns a default ConcreteProduct object.
- This may call the factory method to create a Product object.
ConcreteCreator
- This is a class which implements the Creator class and overrides the factory method to return an instance of a ConcreteProduct.
Let's See with an example
abstract class Vehicle
{
VehicleType vehicleType;
int topSpeed;
int capacity;
int price;
public VehicleType VehicleType
{
get
{
return vehicleType;
}
set
{
vehicleType = value;
}
}
public int TopSpeed
{
get
{
return topSpeed;
}
set
{
topSpeed = value;
}
}
public int Price
{
get
{
return price;
}
set
{
price = value;
}
}
public int Capacity
{
get
{
return capacity;
}
set
{
capacity = value;
}
}
}
public enum VehicleType
{
BIKE,
SCOTTER,
CAR
}
2. Concrete Product
Bike.cs
class Bike : Vehicle
{
public Bike() : base()
{
VehicleType = VehicleType.BIKE;
TopSpeed = 200;
Capacity = 2;
Price = 150000;
}
}
Scotter.cs
class Scotter : Vehicle
{
public Scotter() : base()
{
VehicleType = VehicleType.SCOTTER;
TopSpeed = 100;
Capacity = 2;
Price = 100000;
}
}
Car.cs
class Car : Vehicle
{
public Car() :base()
{
VehicleType = VehicleType.CAR;
TopSpeed = 400;
Capacity = 4;
Price = 700000;
}
}
3.Creator
abstract class VehicleCreationFactory
{
public abstract Vehicle GetVehicle(VehicleType vehicleType);
}
4. Concrete Creator
VehicleFactory.cs
class VehicleFactory : VehicleCreationFactory
{
public override Vehicle GetVehicle(VehicleType vehicleType)
{
switch (vehicleType)
{
case VehicleType.BIKE:
return new Bike();
case VehicleType.CAR:
return new Car();
case VehicleType.SCOTTER:
return new Scotter();
default:
return new Bike();
}
}
}
Our client Demo for Factory Pattern
static void Main(string[] args)
{
VehicleFactory vehicleFactory = new VehicleFactory();
Vehicle bike = vehicleFactory.GetVehicle(VehicleType.BIKE);
Vehicle car = vehicleFactory.GetVehicle(VehicleType.CAR);
Vehicle scotter = vehicleFactory.GetVehicle(VehicleType.SCOTTER);
Console.WriteLine("Bike data: ");
Console.WriteLine("TopSpeed: {0}, Capacity: {1}",
bike.TopSpeed.ToString(),
bike.Capacity.ToString());
Console.WriteLine("Car data: ");
Console.WriteLine("TopSpeed: {0}, Capacity: {1}",
car.TopSpeed.ToString(),
car.Capacity.ToString());
Console.WriteLine("Scotter data: ");
Console.WriteLine("TopSpeed: {0}, Capacity: {1}",
scotter.TopSpeed.ToString(),
scotter.Capacity.ToString());
}
Output
Happy Coding !!
Java, Spring Boot, PL/SQL
6 年Good Job