Click here to Skip to main content
15,440,282 members
Articles / Web Development / ASP.NET
Posted 3 Mar 2014

Tagged as


111 bookmarked

Design Patterns in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.74/5 (55 votes)
3 Mar 2014CPOL8 min read
Part 1 of design patterns in .NET


Design patterns provide general solutions or flexible way to solve common design problems. This article gives you a simple introduction regarding learning and understanding design patterns.

Before starting with design patters in .NET, let’s understand what is meant by design patterns and why it is useful in software programming?

What are Design Patterns in Software Programming?

Design Patterns in object oriented world is reusable solution to common software design problems which occur again and again in real world application development. It is a template or description for how to solve a problem which can be used in many different situations.

"A 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 are used by developers to their particular design to solve their problems. Patterns usage and choice to choose among different design patterns is based on individual need and their problem.

Design patterns are the most powerful tool for software developer. It is important to understand design patterns rather than memorizing its classes, methods and properties. It is also important to learn how to apply pattern to specific problem to get the desired result. This will be required continuous practice of using and applying design patterns in day to day software development. First, identify the software design problem, then see how to address these problems using design patterns and find out the best suited design problem to solve the problem.

There are 23 design patterns also known as Gang of four design patterns (GoF). Gang of four are the authors of the book, “Design Patterns: Elements of Reusable Object Oriented Software”. These 23 patterns are grouped into three main categories based on their:

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

In this article, we are learning and understanding Creational Design patterns in detail including UML diagram, template source code and real world example C#*.

Creational Design patterns provide a way to instantiate single object or group of related objects. These patterns deal with the process of object creation in such a way that they are separated from their implementing system. This way, it provides more flexibility in deciding which object needs to be created or instantiated for a given scenario. There are five such patterns:

1) Abstract Factory

It is used to create a set of related objects or dependent objects. The “family” of objects created by factory is determined at run-time according to the selection of concrete factory class.

Abstract factory pattern acts as super factory which creates other factories. In abstract factory, interface is responsible for creating a set of related objects or dependent objects without specifying their concrete classes.

The UML class diagram below describes an implementation of the abstract factory design pattern:

Image 1

The classes,objects and interfaces used in the above UML diagram is described below:

  1. Client: This class is used Abstract Factory and Abstract Product interfaces to create family of related objects.
  2. Abstract Factory: This is an interface which is used to create abstract products.
  3. Abstract Product: This is an interface which is used to declare type of products.
  4. Concrete Factory: This is a class which implements the abstract factory interface to create concrete products.
  5. Concrete Product: This is a class which implements the abstract product interface to create products.

The following code shows the basic template code of the abstract factory design pattern implemented using C#.NET:

Image 2

Image 3

In the above abstract factory design pattern source code template client has two private fields that hold the instances of abstract product classes. These objects will be accessed by inheriting their base class interface. When a client is instantiated, a concrete factory object is passed to its constructor and populate private fields of client with appropriate data or value.

The Abstractfactory is base class to concrete factory classes which generate or create set of related objects. This base class contains methods definition for each type of object that will be instantiated. The base class is declared as Abstract so that it can be inherited by other concrete factory subclasses.

The concrete factory classes are inheriting from Abstractfactory class and override the method of base class to generate a set of related objects required by client. There can be n number of concrete factory classes depending on software or application requirement.

Abstractproduct is the base class for the types of objects that factory class can create. There should be one base type for every distinct types of product required by client. The concrete product classes are inheriting from Abstractproduct class. Each class contains specific functionality. Objects of these classes are generated by abstractfactory classes to populate client.

Real World Example of Abstract Factory Design Pattern using C#.NET

As an example, consider a system that does the packaging and delivery of items for a web-based store. The company delivers two types of products. The first is a standard product that is placed in a box and delivered through the post with a simple label. The second is a delicate item that requires shockproof packaging and is delivered via a courier.

In this situation, there are two types of objects required, a packaging object and a delivery documentation object. We could use two factories to generate these related objects. The one factory will be responsible for creating packaging and other delivery objects for standard parcels. The second will be responsible for creating packaging and delivery objects for delicate parcels.

Class Client

Image 4

Image 5

AbstractFactory Patterns Form

Image 6


Image 7

In the above example, code creates two client objects, each passing to different type of factory constructor. Types of generated objects are accessed through the client properties.


While studying abstract factory pattern, one question comes in my mind, i.e., what are concrete classes? So I Google searched the same and below is the answer to my question.

Concrete class is nothing but normal class, which is having all basic class features, like variables, methods, constructors, etc.

We can create an instance of the class in the other classes.

2) Singleton

Singleton design pattern is one of simplest design patterns. This pattern ensures that class has only one instance and provides global point of accessing it. The pattern ensures that only one object of a particular class is ever created. All further references to objects of the singleton class refer to the same underlying instance.

There are situations in a project where we want only one instance of the object to be created and shared between the clients. No client can create an instance from outside. It is more appropriate than creating a global variable as this may be copied and leading to multiple access points.

The UML class diagram below describes an implementation of the abstract factory design pattern:

Image 8

In the above singleton patterns UML diagram “GetInstance” method should be declared as static. This method returns single instance held in private instance” variable. In singleton pattern, define all the methods and instance as static. The static keyword ensures that only one instance of object is created and you can call methods of class without creating object.

The constructor of class is marked as private. This prevents any external classes from creating new instances. The class is also sealed to prevent inheritance, which could lead to sub classing that breaks the singleton rules.

The following code shows the basic template code of the singleton design pattern implemented using C#.NET:

The eager initialization of singleton pattern is as follows:

Image 9

Lazy initialization of singleton pattern:

Image 10

Thread-safe (Double-checked Locking) initialization of singleton pattern:

Image 11

In the above code "lockThis" object and the use of locking within the "GetInstance" method. As programs can be multithreaded, it is possible that two threads could request the singleton before the instance variable is initialized. By locking the dummy "lockThis" variable, all other threads will be blocked. This means that two threads will not be able to simultaneously create their own copies of the object.

Real World Example of Abstract Factory Design Pattern using C#.NET

I am trying to apply this pattern in my application where I want to maintain application state for user login information and any other specific information which is required to be instantiated only once and held only one instance.

Class ApplicationState

Image 12

Singleton Pattern Form

Image 13


Image 14

The above sample code creates two new variables and assigns the return value of the GetState method to each. They are then compared to check that they both contain the same values and a reference to the same object.

I hope this article will give you an introduction about design patterns and different types of design patterns used in .NET. In this article, we learned Abstract factory and Singleton design pattern in detail. Remaining patterns of creational design pattern group will be explained to you in my next article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralSome parts are copy paste Pin
Prasanth S16-Jun-16 9:24
MemberPrasanth S16-Jun-16 9:24 
SuggestionGood Explanation. Pin
Amol Lendave22-Dec-15 18:50
professionalAmol Lendave22-Dec-15 18:50 
GeneralMy vote of 4 Pin
ilkerKaran1-Dec-15 1:48
MemberilkerKaran1-Dec-15 1:48 
QuestionHow I can identify the software design problem Pin
Kiran24016-Nov-15 4:57
MemberKiran24016-Nov-15 4:57 
QuestionOther design patterns should also be elaborated, that will be appreciated. Pin
nomi ali27-Oct-15 2:50
professionalnomi ali27-Oct-15 2:50 
QuestionCorrection in First UML diagram Pin
Member 1168648610-Jul-15 4:34
MemberMember 1168648610-Jul-15 4:34 
SuggestionChange "Abstract Product" to "Abstract Factory" in the UML Pin
sandeshjkota26-Oct-14 23:17
Membersandeshjkota26-Oct-14 23:17 
GeneralMy vote of 1 Pin
asifislam4-Sep-14 4:49
Memberasifislam4-Sep-14 4:49 
GeneralRe: My vote of 1 Pin
Sangram Nandkhile22-Jun-15 21:44
MemberSangram Nandkhile22-Jun-15 21:44 
Generalnice description Pin
Rajat_Chanana5-Jun-14 1:01
MemberRajat_Chanana5-Jun-14 1:01 
QuestionI'm pretty sure you don't need locks for a singleton. Pin
nportelli5-Mar-14 10:52
Membernportelli5-Mar-14 10:52 
AnswerRe: I'm pretty sure you don't need locks for a singleton. Pin
nomi ali27-Oct-15 2:51
professionalnomi ali27-Oct-15 2:51 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun3-Mar-14 18:31
MemberHumayun Kabir Mamun3-Mar-14 18:31 
QuestionDoubly checked? Pin
Paulo Zemek3-Mar-14 6:51
MemberPaulo Zemek3-Mar-14 6:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.