Click here to Skip to main content
Licence CPOL
First Posted 1 Jun 2007
Views 15,861
Downloads 378
Bookmarked 17 times

Design Patterns in C#

By | 1 Jun 2007 | Article
Design Patterns in C#

Introduction

This article explains following C# design Patterns with example.

1. Decorator

2. Strategy

3. Singleton

Description:

1. Decorator Pattern

Any application often needs to augment the services provided by methods of some class, perhaps by inserting some preprocessing and post-processing tasks before and after the method calls, respectively.

One way to accomplish this is to bracket each method invocation with calls to functions that achieve the desired effect. However, this approach is not only cumbersome, it also limits the framework's extensibility. For instance, if distinct pre- and post-processing tasks were to be carried out for different clients, the application logic would be obscured by conditional statements, leading to a maintenance nightmare.

The question, then, is how to enhance the functionality offered by a class in a manner that does not cause repercussions in client code. The Decorator pattern is just what's needed.


Consider a common example of creating new account. We usually check for account existence before creating a new one. Here comes the decorator pattern in action. we add additional responsibility to class NewAccount to check for account existence. but while calling CreateNewAccount method we need not bother about checking

for the existing account. Additional responsibility added to NewAccount class will do this every time u call it.

The Decorator pattern thus allows dynamic and transparent addition and removal of responsibilities without affecting client code. It is particularly useful when a range of extensions or responsibilities can be applied to existing classes, and when defining subclasses to accommodate all those extensions is impractical.

//Base class 
public class NewAccount
{
    //Virtual methods to be overridden in decorator class
}
//Decorator class: that adds new responsibilities to base class
public class NewAccountDecorator : NewAccount 
{
    //overridden methods that extends the functionality of base class
}

2. Strategy Pattern

Applications are often written so that the way they perform a particular task varies, depending on user input, the platform it's running on, the environment it's been deployed in, and so on. An example is method of payment : the customer may either pay by cash or card in that case the application should choose it's functionality depending upon the user choice given.

In short the strategy pattern allows us choose from the multiple alogorithms to perform the operation.

PaymentMethod objMethodOfPayment = new PaymentMethod();
switch objMethodOfPayment .SelectedText)
{
    case "Cash": 
        objMethodOfPayment .setMethodOfPayment(new PayByCash());
        break;
    case "Card": 
        objMethodOfPayment .setMethodOfPayment(new PayByCard());
        break;
}
objMethodOfPayment .Pay(amount);


3. Singleton Pattern

In some situations we may want to allow single instance of an class to create thoughout the application.The Singleton class allows us to implement single instance of the class.

One other advantage to this pattern is that creation of the singleton can be delayed until it is actually needed. A variable declared at global scope will be created on startup regardless of whether it is needed—it may very

well be that the object isn't always needed as shown below

private static SingletonClass instance;
public static SingletonClass Instance()
{
    // Create instace if not alredy present 
    if (instance == null)
    {
        instance = new SingletonClass();
    }
    return instance;
}

License

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

About the Author

KundanKumar Ugale

Web Developer

India India

Member

KundanKumar Ugale
 
kundan.ugale@gmail.com
+91 9860526936
Mumbai(India)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalsmall mistake. PinmemberMephistopehel10:24 20 Nov '07  
GeneralLittle note PinmemberOvchar864:29 1 Jun '07  
GeneralRe: Little note Pinmembereasiaccess5:47 1 Jun '07  
GeneralRe: Little note PinmemberOvchar865:58 1 Jun '07  
GeneralRe: Little note PinsitebuilderUwe Keim9:49 1 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 1 Jun 2007
Article Copyright 2007 by KundanKumar Ugale
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid