Design Patterns in C#

This article describes design patterns for software development. Basically there are the following three categories of design patterns:
  1. Creational
  2. Behavioral
  3. Structural

What is a  Design pattern?
Christopher Alexander says, "Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.". Even though Alexander was talking about patterns in buildings and towns, what he says is true about object-oriented design patterns. Our solutions are expressed in terms of objects and interfaces instead of walls and doors, but at the core of both kinds of patterns is a solution to a problem in a context.
Let's proceed with getting some idea of all these patterns.
Creational Pattern
Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented. A class creational pattern uses inheritance to vary the class that's instantiated, whereas an object creational pattern will delegate instantiation to another object.
Creational patterns become important as systems evolve to depend more on object composition than class inheritance. As that happens, emphasis shifts away from hard-coding a fixed set of behaviors toward defining a smaller set of fundamental behaviors that can be composed into any number of more complex ones. Thus creating objects with specific behaviors requires more than simply instantiating a class. 
Behavioral Pattern
Behavioral patterns are concerned with algorithms and the assignment of responsibilities among objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication among them. These patterns characterize complex control flow that's difficult to follow at run-time. They shift your focus away from the flow of control to let you concentrate just on the way objects are interconnected.
Structural Pattern
Structural patterns are concerned with how classes and objects are composed to form larger structures. Structural class patterns use inheritance to compose interfaces or implementations. As a simple example, consider how multiple inheritance combine two or more classes into one. The result is a class that combines the properties of its parent classes. This pattern is particularly useful for making independently developed class libraries work together.
Let's have a look at a creational pattern.
A few popular examples of creational design patterns are:
  • Singleton design pattern
  • Factory design pattern
  • Prototype design pattern
Singleton Design Pattern
A singleton pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance and provides a global point of access to it.
A singleton pattern is used when there must be exactly one instance of a class and it must be accessible to clients from a well-known access point.
When the sole instance should be extensible by subclassing and clients should be able to use an extended instance without modifying their code.
Let's have a look at the following code snippet.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Collections;  
  6. using System.Data.SqlClient;  
  7. using System.Data;  
  8. using System.Diagnostics;  
  10. namespace Sample  
  11. {  
  13.     //Singletone Design patern in C#  
  15.     public class SingleTone  
  16.     {  
  18.         private SingleTone()  
  19.         {  
  21.         }  
  23.         private static SingleTone Single;  
  25.         public Int32 Statcount { getset; }  
  28.         public static SingleTone ReturnObject()  
  29.         {  
  30.             if  
  31.             (Single != null)  
  32.             {  
  33.                 return  
  34.                 Single;  
  35.             }  
  36.             else  
  37.             {  
  38.                 Single = new SingleTone();  
  39.                 return  
  40.                 Single;  
  41.             }  
  44.         }  
  46.     }  
  48.     class Program  
  49.     {  
  51.         static void  
  52.         Main(string[] args)  
  53.         {  
  54.             SingleTone s = SingleTone.ReturnObject();  
  55.             s.Statcount = 200;  
  57.             SingleTone s1 = SingleTone.ReturnObject();  
  58.             s1.Statcount = 300;  
  60.             Console.WriteLine(s.Statcount);  
  61.             Console.ReadLine();  
  63.         }  
  65.     }  
  67. }  

In this article we have learned about design patterns in C# and a basic example of the Singleton Design Pattern. In my future articles we will see some more examples of design patterns.

Similar Articles