Design Patterns in C#


The Design pattern is a solution to software design problems you find in real world application development.It is a way in which software are build so that the code may be reusable when required.The selection of appropriate design pattern is very necessary, before starting the development of any software.It is not compulsory that you have to select design pattern from the available list, you can make your own custom pattern also.

While solving  a particular problem, you come up with a solution  that can be reused at many of the solutions on which you are working then you can select that structure of solving the problem as your custom design pattern.Make your own class libraries and share it within your company.It will be your custom design pattern.

  •  Patterns are about reusable designs and interaction of objects.
  •  It is the best practice used by experienced software developers.
          Patterns  are categorized in 3 major groups:
  1. Creational Pattern
  2. Structural Pattern
  3. Behavioral Pattern
There is a big list of design pattern belonging to each of the above group, we are not going to discuss all of them. Instead, we are going to check out most widely used design patterns in software development.You can check out the list of all the available design pattern at MSDN. The most used design patterns are listed below.
  1. Singleton Pattern(Creational Pattern) 
  2. Factory Method Pattern(Creational Pattern)
  3. Observer Pattern(Behavioral Pattern)
  4. Dependency Injection
It is difficult to cover all the patterns in a single post, so in this post we are going to explore Singleton Pattern.

Singleton Pattern:

 Singleton pattern is used when we want to provide global access to the objects, but the constraint  is that there must be only single instance available or we can say we want to implement a class in a way that can be accessed globally, but we can just only make a single instance of that class.In practice, there are many different ways available to implement singleton pattern.but we are going to implement it using the simplest way available.



Implementation: 

Firstly, we have to create a class(singleton) which doesn’t allow to create an object from outside of that class, So this can be achieved using a private constructor.

1
2
3
4
5
6
class singleton
{
  private singleton()
  {
  }
}

Now we cannot create the instance of the above class outside that class. This will not fulfill our requirement of exposing an only single instance of the class. To achieve that we have to instantiate the class inside of that class and store that instance in a private static variable. After implementing this requirement, our class will look like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
   class singleton
    {
        private static singleton _instance;
        private singleton()
        {
        }
        public static singleton Instance()
        {
            _instance = new singleton();
            return _instance;           
        }
     }

Now every time when  Instance() method called it will create a new object for that class so this is not what we actually need.We just need a single instance of that class to be created. So we need to make a simple check to limit the creation of an instance of the singleton class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class singleton
{
     private static singleton _instance;
     private singleton()
     {
     }
     public static singleton Instance()
     {
         if (_instance == null)
         {
             _instance = new singleton();
         }
         return _instance;
     }
}

Yes, we are done now. It's a singleton class now, with the only single instance is exposed globally for use.Now let's make a simple example to check that our singleton class is really working or not.In this example will try to make two make to an instance of the class and then compare both instance to see if they both are same or not.If both are same then it will return "Singleton is applied" otherwise "Singleton is not applied".


Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Program
    {
        static void Main(string[] args)
        {
            singleton s1 = singleton.Instance();
            singleton s2 = singleton.Instance();
            if (s1 == s2)
            {
                Console.WriteLine("Singleton is appiled");
            }
            else
            {
                Console.WriteLine("Singleton is not applied");
            }
        }
    }

    class singleton
    {
        private static singleton _instance;
        private singleton()
        {
        }
        public static singleton Instance()
        {
            if (_instance == null)
            {
                _instance = new singleton();
            }
            return _instance;
        }
    }


Output:


It's done our singleton class is working as expected.In our next post, we will continue with one of the other remaining design patterns.If you have learned anything from our post please do comment, follow us on twitter and like our Facebook page. 








Comments

  1. Nice Blog.
    Read the blog in details with all the points that are mentioned and I believe that this is the best article I've gone through. Everyone should check this blog as a tutorial.
    Thanks for sharing such a great blog.
    Asp Dot Net Core

    ReplyDelete

Post a Comment