fbpx

Introduction:

Design patterns are critical in software development by providing reusable and efficient solutions to common problems. In object-oriented programming (OOP) with C#, several design patterns utilize OOP concepts to create maintainable, modular, and testable code. This article will delve into three popular OOP design patterns in C#: Repository Pattern, Singleton Pattern, and Factory Method Pattern. We’ll explore their purpose, examine code examples, and discuss real-world use cases.

1. Repository Pattern

The Repository Pattern is a widely-used design pattern that abstracts the data access layer of an application. By isolating data access logic from the rest of the application, the Repository Pattern enforces separation of concerns, making it easier to switch data sources or implement unit testing.

Example:

public interface IRepository<T>
{
    // Repository methods
}

public class ProductRepository : IRepository<Product>
{
    // Implement repository methods using a data source
}

public class ProductService
{
    private readonly IRepository<Product> _productRepository;

    public ProductService(IRepository<Product> productRepository)
    {
        _productRepository = productRepository;
    }

    // Perform business logic using the repository
}

Real-world use case: In e-commerce applications, the Repository Pattern can manage products, orders, and customer information across different data sources, such as databases or APIs.

2. Singleton Pattern

The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it. It is beneficial when controlling access to shared resources like a configuration manager or a logging service.

Example:

public sealed class Logger
{
    private static readonly Lazy<Logger> _instance = new Lazy<Logger>(() => new Logger());

    private Logger() { }

    public static Logger Instance
    {
        get { return _instance.Value; }
    }

    public void Log(string message)
    {
        // Log the message
    }
}

// Usage
Logger.Instance.Log("This is a log message.");

Real-world use case: Applications can employ the Singleton Pattern for logging, configuration management, or database connections, ensuring that only one instance exists for a shared resource.

3. Factory Method Pattern

The Factory Method Pattern defines an interface for creating an object but allows subclasses to decide which class to instantiate. This pattern fosters loose coupling by eliminating the need to bind application-specific types to the code.

Example:

public interface IVehicle
{
    void Drive();
}

public class Car : IVehicle { /* Car driving logic */ }
public class Truck : IVehicle { /* Truck driving logic */ }

public abstract class VehicleFactory
{
    public abstract IVehicle CreateVehicle();

    public void DoSomethingWithVehicle()
    {
        IVehicle vehicle = CreateVehicle();
        vehicle.Drive();
    }
}

public class CarFactory : VehicleFactory { /* Create Car */ }
public class TruckFactory : VehicleFactory { /* Create Truck */ }

Real-world use case: Applications managing different object types, such as document readers for various file formats or transport systems with multiple vehicle types, can utilize the Factory Method Pattern to create objects without exposing the creation logic.

Conclusion:

By mastering the Repository Pattern, Singleton Pattern, and Factory Method Pattern in C#, developers can leverage OOP concepts to write maintainable, modular, and testable code. These design patterns offer efficient solutions to common software design problems and are essential tools for every C# developer.

A Geek by nature, I love to work on challenging development projects. I have been in Programming for last 13 years and still too young to learn anything new. I have exceptional command using AngularJS and Python/.Net/NodeJS.

Leave a Reply

Your email address will not be published. Required fields are marked *