Showing posts with label C# Design Patterns Tutorial Real-time example of Factory Pattern. Show all posts
Showing posts with label C# Design Patterns Tutorial Real-time example of Factory Pattern. Show all posts

Monday, September 29, 2025

🏭 Factory Design Pattern in C# with Real-Time Example

πŸ“Œ What is the Factory Design Pattern?

The Factory Design Pattern is a creational pattern used in object-oriented programming to abstract the process of object creation. Instead of instantiating classes directly using new, the Factory Pattern provides a method that returns instances of different classes based on input parameters.

This pattern is especially useful when:

  • The exact type of object to create is determined at runtime.
  • You want to encapsulate object creation logic.
  • You need to adhere to the Open/Closed Principle (open for extension, closed for modification).

πŸš— Real-Time Example: Vehicle Factory in C#

Imagine you're building a transportation management system that handles different types of vehicles: Car, Bike, and Truck. Each vehicle has its own behavior, but the client code shouldn't worry about how these objects are created.

✅ Step-by-Step Implementation

1. Define a Common Interface

public interface IVehicle
{
    void Start();
}

2. Create Concrete Implementations

public class Car : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Car started.");
    }
}

public class Bike : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Bike started.");
    }
}

public class Truck : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Truck started.");
    }
}

3. Implement the Factory Class

public static class VehicleFactory
{
    public static IVehicle GetVehicle(string vehicleType)
    {
        switch (vehicleType.ToLower())
        {
            case "car":
                return new Car();
            case "bike":
                return new Bike();
            case "truck":
                return new Truck();
            default:
                throw new ArgumentException("Invalid vehicle type");
        }
    }
}

4. Client Code

class Program
{
    static void Main(string[] args)
    {
        IVehicle vehicle1 = VehicleFactory.GetVehicle("car");
        vehicle1.Start();

        IVehicle vehicle2 = VehicleFactory.GetVehicle("bike");
        vehicle2.Start();
    }
}

🎯 Benefits of Using Factory Pattern in C#

  • Encapsulation: Object creation logic is hidden from the client.
  • Scalability: Easily add new vehicle types without changing client code.
  • Maintainability: Centralized object creation makes debugging and updates easier.
  • Loose Coupling: Promotes interface-based programming.

πŸ“ Final Thoughts

The Factory Design Pattern is a cornerstone of clean, scalable software architecture. Whether you're building enterprise applications or small utilities, this pattern helps you write flexible and maintainable code.


Don't Copy

Protected by Copyscape Online Plagiarism Checker

Pages