Object-Oriented Design Patterns in C#

Object-Oriented Design Patterns in C#

Understanding Object-Oriented Design Patterns in C#

Object-Oriented Design Patterns are proven solutions to common software design problems. They help make code more maintainable, reusable, and flexible. In C#, these patterns provide a structured approach to building robust and scalable applications. Let's explore some of the most widely used design patterns with examples in C#.

1. Creational Patterns

Creational patterns focus on object creation mechanisms. They help manage and decouple object instantiation.

Singleton Pattern

Ensures a class has only one instance and provides a global point of access to it.


public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();
    private Singleton() { }

    public static Singleton Instance
    {
        get { return instance; }
    }
}

    

Factory Method Pattern

Defines an interface for creating objects but allows subclasses to alter the type of objects that will be created.


public interface IProduct
{
    void Display();
}

public class ConcreteProductA : IProduct
{
    public void Display() => Console.WriteLine("Product A");
}

public class ConcreteProductB : IProduct
{
    public void Display() => Console.WriteLine("Product B");
}

public class ProductFactory
{
    public static IProduct CreateProduct(string type)
    {
        return type switch
        {
            "A" => new ConcreteProductA(),
            "B" => new ConcreteProductB(),
            _ => throw new ArgumentException("Invalid type")
        };
    }
}

    

2. Structural Patterns

These patterns help organize classes and objects into larger structures while maintaining flexibility and efficiency.

Adapter Pattern

Allows incompatible interfaces to work together by converting one interface into another.


public interface ITarget
{
    void Request();
}

public class Adaptee
{
    public void SpecificRequest() => Console.WriteLine("Specific Request");
}

public class Adapter : ITarget
{
    private readonly Adaptee _adaptee;

    public Adapter(Adaptee adaptee)
    {
        _adaptee = adaptee;
    }

    public void Request()
    {
        _adaptee.SpecificRequest();
    }
}

    

3. Behavioral Patterns

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.

Observer Pattern

Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.


public class Subject
{
    private readonly List observers = new();

    public void Attach(IObserver observer) => observers.Add(observer);
    public void Detach(IObserver observer) => observers.Remove(observer);

    public void Notify()
    {
        foreach (var observer in observers)
        {
            observer.Update();
        }
    }
}

public interface IObserver
{
    void Update();
}

public class ConcreteObserver : IObserver
{
    public void Update() => Console.WriteLine("Observer Updated");
}

    

Conclusion

Object-Oriented Design Patterns are essential tools for any C# developer. They promote better design principles and allow for easier maintenance and expansion of software systems. By understanding and implementing these patterns, you can create more efficient and flexible code.

Happy coding!

Any Query / Enrollment Request



Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50