Design patterns in software development

Let’s talk about software design patterns, one of the finest topics in software development, or probably the topic that will make us stronger about the deep understanding of software development.

Design pattern is the common solution (standard way of implementing objects) to a commonly occurring problem in software design, the simplest way to remember the design pattern, how different ways we can create object of any class (product) and what problem does it solve! in each design pattern the object creation and implementation is done differently.

Since early days there are many software design patterns has been practiced, some of them has been largely used in software development, some are not so popular, some are losing importance, when some more new design pattern are introduced.

Common design patterns c# examples

Here we talk about most commonly used five design patters that we often use in c# development.

There are many design patterns in software development and they are categorized like creational (abstract factory, prototype, singleton), structural(adapter,bridge, composite, proxy), behavioural(command, iterator, observer, visitor) etc. here we learn few commonly used design patterns that we often use in our software development.

  1. Dependency Injection Design Pattern

    In start-up file, we need to register the dependent class and interface.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IMyDependency, MyDependency>();
        services.AddRazorPages();
    }
    

    We can create an instance-using interface and the constructor of page or controller (in MVC)

     
    public class Index1Model : PageModel
    {
        private readonly IMyDependency _myDependency;
        public Index2Model(IMyDependency myDependency)
        {
            _myDependency = myDependency;            
        }
        public void OnGet()
        {
            _myDependency.WriteMessage("Index2Model.OnGet");
        }
    }
    

    Here is an example of how we can implement Dependency Injection in ASP.NET Core.

  2. Singleton Design Pattern

    In singleton design pattern, we do not allow a new instance to be created by consumer class, instead we create the instance internally, and let them access all methods through that, means there will be always one instance of that class, no matter how many different class access that instance.

    class ProductService
    {
        private static ProductService instance = null;
        private static object lockthis = new object();
    
        private ProductService()
        {
        }
    
        public static ProductService Instance
        {
            get
            {
                lock (lockthis)
                {
                    if (instance == null)
                    {
                        instance = new ProductService();
                    }
                    return instance;
                }
            }
        }
    
        public string WelCome()
        {
            string _strM = "Welcome to Class";
            return _strM;
        }
    }
    
  3. CQRS (Command Query Responsibility Segregation)

    CQRS stands for Command and Query Responsibility Segregation, a this design pattern separates reading and updating data, so the implementation become more cleaner, provide better performance, easy to apply authorization logic and role based security.

    Learn more about CQRS Pattern

    Here is an example of how we can implement CQRS design pattern in Microservices real-time solution using asp.net core framework.

  4. Factory Design Pattern

    Creating one simple interface "ICarFactory", whenever any new type of car class will be crated, that will inherit from this ICarFactory interface.

    public interface ICarFactory
    {
        void Drive(int miles);
    }
    
    public class Maruti : ICarFactory
    {
        public void Drive(int miles)
        {
            Console.WriteLine("Maruti : " + miles.ToString() + "km");
        }
    }
    
    public class Honda : ICarFactory
    {
        public void Drive(int miles)
        {
            Console.WriteLine("Honda : " + miles.ToString() + "km");
        }
    }
    

    Now we create an abstract class called CarFactory, which will return (an abstract type of ICarFactory) a car from GetCar method.

    public abstract class CarFactory
    {
        public abstract ICarFactory GetCar(string car);
    }
    

    Finally, create a concrete class, which will return the correct car type based on parameter.

    This class will be consumed by client class.

        public class ConcreteCarFactory : CarFactory
        {
            public override ICarFactory GetCar(string car)
            {
                switch (car)
                {
                    case "maruti":
                        return new Maruti();
                    case "honda":
                        return new Honda();
                    default:
                        throw new ApplicationException(string.Format("Car '{0}' cannot be created", car));
                }
            }
        }
    

    Here is how we create a new instance of above concrete factory class, and get the car type we want.

    CarFactory factory = new ConcreteCarFactory();
    
    ICarFactory maruti = factory.GetCar("Maruti");
    maruti.Drive(50);
    
    
    ICarFactory honda = factory.GetCar("Honda");
    honda.Drive(20);
    
  5. Abstract Factory Pattern

    Abstract factory pattern is also creational pattern like factory pattern, The main difference is, in factory pattern we creates object through inheritance, in abstract factory pattern we creates object through composition.

    Let’s learn difference between factory and abstract factory pattern.


Good to Read
You may also read
How to use DevOps Azure Service
DevOps Azure Service Tutorial
Software Testing Tutorial
software testing tutorial guide for beginners
AI and Machine Learning
AI and Machine Learning