AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1289&pId=-1
Understanding Design Pattern Using C#
page
by Srinivas Jadhav
Feedback
Average Rating: 
Views (Total / Last 10 Days): 16949/ 27

Introduction

Design Patterns are about design and interaction of objects. They provide a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges. It is a way to pursue an intent that uses classes and their methods in an OO language.

Design Patterns can solve problems by providing a framework for building a cleaner and more efficient application. I will be covering the most frequently used patterns in the Industry.

Categories of Design Patterns

Design patterns are covered in 5 categories.

1. Interfaces

          * Adapter: Convert programming interface of one class into that of another. It is used whenever we want unrelated classes to work together in a single program. There are two ways to do this.                                                                                                           

By Inheritance

It is used when client specifies its requirements in an interface and subclasses an existing class.

Listing 1 

using System; 
public class AdapterPattern
{
static void Main()
{
   //Non adapted Animal
   Animal nonAdapted = new Animal("Lion","Wild");
   nonAdapted.Display();   //Adapted Animal
   Animal adapted = new AdaptedAnimal("Dog","Pet");
   adapted.Display(); Console.Read();
 }
}
 public class Animal
{
  public string name;
  public string type;
  public Animal(string name, string type)
 { 
   this.name = name;
   this.type = type;
  }
  public virtual void Display()
  {
    Console.WriteLine("\nNon-Adapted Animal name is '{0}' and type is '{1}'",name, type);
  }
}
 //Adapter
 public class AdaptedAnimal : Animal
 {
   public AdaptedAnimal(string name,string type) : base(name,type)   {}
   public override void Display()
   {
     //Adaptee
    Console.WriteLine("\nAdapted Animal name is '{0}' and type is '{1}'",name, type);
    //Adaptee //Any thing done here will be adaptee
   }
}

The main purpose of the above listing is to map the interface of one class onto another so that they can work together. It demonstrates that whether an Animal can be Adapted or Non-Adapted.                                                                                  

By Object Composition

It creates an object adapter that forwards a client's calls to an instance of the existing class. Use this when the client does not provide the interface it requires.

          * Facade: Provide a simple interface into a collection of classes

          * Composite: Define an interface that applies to individual objects and groups of objects.

2. Responsibility

          * Singleton: A class of which only a single instance can exist.

          * Observer: A way of notifying change to a number of classes.

          * Proxy: An object representing another object.

3. Construction

          * Factory Method: Creates an instance of several families of classes.

          * Abstract Factory: Creates an instance of several families of classes.

4. Operations

          * State: Alter an object's behavior when its state changes.

          * Command: Encapsulates a command request as an object.

5 Extensions

          * Iterator: Sequentially accesses the elements of a collection.

          *Decorator: Adds responsibilities to objects dynamically.

Summary

This article examined the application of Design Patterns using C#.



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 5:21:01 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search