Understanding Design Patterns

Introduction

I hope you are doing good. In this article, we are going to understand about few vital design patterns.

What is Design Pattern and Why do we need it? 

Is it not good to do coding without any design pattern? The answer is yes we can do it, however, firstly we have to understand here as a Design Pattern is not a technology, it's just a reusable solution that addresses a few known issues. such as bumps at the last minute, we can think of a broader vision, how the application can enhance with multiple parties independently, how to bring different technology also how to make all resources to utilize. All stack has their own pros and cons, based on our requirement on how to bring them into the board. 

Also, note that the design pattern is not mean the up-gradation of the technology stack. It purely focuses on the below 'ility'

  1. Loosely couple 
  2. Scalability
  3. Extensibility
  4. Performance
  5. Modularity

In this article, we are going to focus on 5 design patterns 

  1. Singleton Pattern
  2. Facade Pattern
  3. Observer Pattern
  4. Proxy Pattern
  5. Pub-Sub Pattern

1. Singleton Pattern

Singleton design pattern is useful in most software applications design. By using the Singleton design pattern we can maintain only one instance for a particular class through the single access point. Also, it knows knowns as a single instance across the lifetime. 

Through out life time, singleton always as "I yam what i am!"

Please find the below code snippet to understand a bit more

public sealed class SingletonPattern
{
    private static SingletonPattern singleton;

    private SingletonPattern() { }
    public static SingletonPattern GetInstance()
    {
        singleton =  singleton ?? new SingletonPattern();
        return singleton;
    }
}
static void Main(string[] args)
{
    SingletonPattern firstObject = SingletonPattern.GetInstance();
    SingletonPattern secondObject = SingletonPattern.GetInstance();
    if(firstObject == secondObject)
    {
        Console.WriteLine("Two instances are same, Singleton pattern is working fine");
    }

    Console.ReadLine();
}

 2. Facade Pattern

A facade is kind of gateway which accepts input from the end-user, then internally will take care to call the rest of the service 

Its acts as a gateway, which accepts request and route it to appropriate recipient.

3. Observer Pattern

This pattern is used to notify the object changes to all its dependants.

real time example.,

  1. In React-Redux, while state  chanes, the dependent  view will be notified and render the same.
  2. If stock price increases , the recent swift will notify to the investors.

4. ProxyPattern

The proxy pattern is one which acts as an interface for real entity. 

real time example.,

  1. when you are generating proxy for your WCF service
  2. ATM is acts as a proxy for bank
  3. In schooling days, we do attendance proxy for our friends :)

5. Pub-Sub Pattern

Another name of Pub-Sub pattern is called as EDA, where brokers are acts as intermediate and producer will publish the events to broker, where subscriber will consume it from their end. This approach is niche nowadays , because you can avoid tight coupling between application or microservices.

real time example.,

  1. Toll gate, where the system capture the vehicle number and pass it to broker, rest of the application will take care to proceed further. 
  2. Asyncronous operation, such as fire and forgot call, sending an email / text message or logging application.


Similar Articles