.net.net FrameworkASP.netC#.net

Design Patterns overview in C# (Part – 1)

In this article, we will discuss the overview of Design patterns in C#.

Let’s understand what is Pattern?

Pattern is a recurring solution to a problem in a context“.

“Each pattern describes a problem which 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.”

–Christopher Alexander – A Pattern Language

Patterns solve software structural and non functional problems.

Structural problems like:

  • Abstraction
  • Encapsulation
  • Data Hiding
  • Separation of Concerns
  • Separation of Interface and Implementation
  • Single point of reference
  • Coupling and Cohesion etc..

Non Functional problems like:

  • Efficiency
  • Reliability
  • Interoperability
  • Testability
  • Reusability etc..

So now let’s understand what is design Pattern?

Software Design Pattern Definition (Wikipedia):

A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system

Design Patterns provide standardized and efficient solutions to software design and programming problems that are re-usable in your code and it provides general solutions or flexible way to solve common design problems.

Types of Design Pattern:

There are 3 types of Design Pattern.

  • Creational Patterns
    • This type of pattern address problems of creating an object and separating it from operations
  • Structural Patterns
    • This type of pattern address problems of using object oriented constructs to organize classes and objects
  • Behavioral Patterns
    • This type of pattern address problems of assigning responsibilities to classes

Classification of Patterns:

Creational Design Patterns Structural Design Patterns Behavioural Design Patterns
1. Singleton 1. Adapter 1. Chain of Responsibility
2. Factory Method 2. Bridge 2. Command
3. Abstract  Factory 3. Composite 3. Interpreter
4. Prototype 4. Decorator 4. Iterator
5. Builder 5. Façade 5. Mediator
6. Flyweight 6. Memento
7. Proxy 7. Observer
8. State
9. Strategy
10. Visitor
11. Template Method


Creational Design Patterns:

We will discuss creational patterns in detail.


  • Ensure a class only has one instance.
  • Provide a global point of access to it.

Let’s see how to handle this.

1.Create a public Class (name SingleTonTest).

2.Define its constructor as private.

3.Create a private static instance of the class (name singleTonObject).

4.Now write a static method (name InstanceCreation) which will be used to create an instance of this class and return it to the calling method.

5.Create a public method in this class.

6.Now we will create an another Class (name Program).

7.Create an entry point to the above class by having a method name Main.

Factory Method:

  • Define an Interface for creating an object but let subclasses decide which class to instantiate
  • Lets a class defer instantiation to subclasses

Let’s see how to achieve this.

1.Create an interface customer for creating the objects.

  1. Now create a class customerClass which implements from customer interface.

  2. Create an abstract class classA and declares the factory method, which returns an object of type Customer.

4.Now create a class classB which implements the classA  and overrides the factory method to return an instance of a customerclass.

Abstract Factory Pattern:

  • Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Abstract Factory patterns acts a super-factory which creates other factories. This pattern is also called as Factory of factories

AbstractFactory Design Pattern
This is an interface which is used to create abstract product
This is a class which implements the AbstractFactory interface to create concrete products.
This is an interface which declares a type of product.
This is a class which implements the AbstractProduct interface to create product.
This is a class which use AbstractFactory and AbstractProduct interfaces to create a family of related objects.


Let’s look at the Implementation:

  1. Abstract Factory use Factory design pattern for creating objects. But it can also use Builder design pattern and prototype design pattern for creating objects. It completely depends upon your implementation for creating objects.
  2. Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  3. When Abstract Factory, Builder, and Prototype define a factory for creating the objects, we should consider the following points :
    1. Abstract Factory use the factory for creating objects of several classes.
    2. Builder use the factory for creating a complex object by using simple objects and a step by step approach.
    3. Prototype use the factory for building a object by copying an existing object.

Prototype Pattern:

  • Prototype pattern specifies the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.
  • It is used to create a duplicate object or clone of the current object to enhance performance.

Prototype Pattern


This is an interface which is used for the types of object that can be cloned itself.
This is a class which implements the Prototype interface for cloning itself.


Let’s see how to achieve this.

Builder Pattern:

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • In other words,you will have to design the system in such a way that the client application will simply specify the parameters that should be used to create the complex object and the builder will take care of building the complex object.


Builder Pattern in dot net



This is an interface which is used to define all the steps to create a product
This is a class which implements the Builder interface to create complex product.
This is a class which defines the parts of the complex object which are to be generated by the builder pattern.
This is a class which is used to construct an object using the Builder interface.


Let’s see how to achieve this.

The creation of objects should be independent from the way the object’s parts are assembled.
Runtime control over the creation process is required.

We will discuss on structural design pattern in next article.

© 2015, www.techkatak.com. All rights reserved.

One thought on “Design Patterns overview in C# (Part – 1)

Comments are closed.