Implementing design patterns in .Net

Design patterns are standard solutions for problems faced while designing software architecture. It was basically conceptualized by a group of architects (known as Gang of Four) who have documented their experience in terms of standardized solutions as patterns.

The basic component involved in the design pattern of object-oriented software architecture is a class. Each pattern involves a set of classes interacting in a pre-defined way to provide a solution for a specific problem.

Design patterns are categorized into following three types based on the kind of service they provide:

Creational – include Abstract Factory, Factory, Singleton, etc.

Structural – Include Façade, Bridge, Adapter, etc.

Behavioral – Include Observer, State, Mediator, etc.

Design patterns in .Net framework

The class library in the framework of Microsoft .Net has used patterns in many of its internal class structure. Some of the patterns like Iterator, Observer, etc. are in-built within the programming language like C# and

Following are some examples how design patterns can be implemented in .Net:

a) Iterator

Iterator is a pattern to sequentially access the elements of a collection. There are many forms of implementation of this pattern used in the .net framework. For example, the language construct in C# for iterating a collection, ‘foreach’ loop traverses an array of elements. This internally uses an iterator pattern to achieve the task. Also, there is a provision to use IEnumerable interface for any collection type of objects to be traversed. The method, GetEnumerator of IEnumerable interface returns object which implements IEnumerator. This object has methods to access the individual element of the collection. Hence, the Iterator pattern is generic and allows easy traversal of the collection without adding any complexity to the collection class.

b) Observer pattern

It is a behavioral pattern where an object (Subject) can notify its dependent objects (Observer) on change of its state (event). An example of this usage would be to see the different views (bar-chart, pie chart, etc.) of a spreadsheet getting updated on change in the data in spreadsheet.
This pattern has been implemented in .Net as ‘delegates’ and ‘events’ in the context of asynchronous programming. The Subject class raises ‘event’ on occurrence of an action will trigger to call the routine called ‘EventHandler’ of the Observer class. Thus, the synchronous method for handling the event is called asynchronously.

Syntax for declaring the delegate method is :

Public delegate void Event1Handler();

To implement delegates, following steps need to be done:

• Declare the event handlers at global level
• Each Observer class needs to register the correct delegate method with the corresponding event to handle them in the EventHandler
• Write the code for handling the respective event

c) Singleton

Singleton implies ONE single instance of an object throughout an instance of an application. For example, there can be one instance of windows directory service throughout the network.

To implement this pattern in .Net

• Create a wrapper class which has the object whose only one instance needs to exist. Declare this object as ‘shared’ instance in the Private section of the wrapper class.
• Define a ‘private’ constructor to that class so that the creation of these objects is restricted from outside world.
• Provide static method , declared as Public member, to access the singleton object

d) Prototype

Prototype is a creational pattern in which a fully initialized instance needs to be copied or cloned. In .Net, this pattern is implemented in the feature called Cloning. It is achieved by using ICloneable of the System namespace. ‘Clone’ method allows a shallow copy which means changes made to the cloned object will update the source object since the clone is just the reference of the source object.

e) MVC (Model View Controller)

This pattern is mainly aimed to decouple the User Interface from Data. With this, the possible views underlying the data are always updated on any change in the data. The different elements of this pattern can be implemented in .Net as detailed below:


This part represents the data maintenance section of the application. It involves the business logic components, database handling objects, process components, etc. This layer can be implemented using Dataset, classes, etc. irrespective of the type of application being Windows or web based.


This section represents the User interface of the application, displaying part or all the data. This layer needs to take care of the aesthetics necessary for user-friendliness of the application. Desktop applications can make use of the data grid and user interface controls provided by Windows Forms collection. In case of Web based applications, .aspx, .asmx, etc. files are used for programming the logic for displaying data in the browser.


This element either changes the model or views based on the event generated from any input to the application. In, PageController is the controller for every logical page. It takes care of responding to user input and updates both model and view. System.Web.UI.Page is used as a base class for all PageControllers. In case of desktop applications, Controller can be designed using the concept of ‘event’ and ‘delegate’ as discussed in Observer pattern.

Benefits of using design patterns in .Net:

• Enhance the framework of the application by leveraging the solutions already framed by architects and hence reduce time and effort of development
• Achieve extensibility of code and hence better re-use
• Provide code that is efficient and more easy to maintain
• Provide solutions in object-oriented approach
• Promote further usage of patterns as a work style during development
• Acts as a means to frame a flexible architecture to cater to agile business needs of industry

The patterns suggested by them can be used as a guideline than as a rule. Hence, it is always better to evaluate the impact of using the selected design pattern in combination with existing .Net classes before actually finalizing the application architecture.


Add to My Yahoo!

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

| All about Conceptual Analysis on .NET Remoting | Building desktop applications in .Net | Building Distributed Applications Efficiently Using .Net Remoting | C# (C Sharp) Unified Type System: Reference Types | C# (C Sharp) Unified Type System: Value Types | Data access in .Net | Delegates Vs Interfaces in C# (C Sharp) | How is Integration between Java and .NET Achieved | How is Versioning Achieved in C# (C Sharp) | Implementing Authorization and authentication in | Implementing design patterns in .Net | List of Query Keywords in C# (C Sharp) |


| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.