Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Ads by Lake Quincy Media
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Design & Architecture » Design Patterns in C#

Design Patterns in C#

To define design patterns in simple words they are "popular solutions for common design problems". They are very helpful in designing architecture and they also increase ease of communication among the developers.

Total page views :  9916
Total downloads :  79
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
PatternsCode.zip
 
Become a Sponsor

Design patterns have picked up a lot of importance off late and rightfully so. To define design patterns in simple words they are "popular solutions for common design problems". They are very helpful in designing architecture and they also increase ease of communication among the developers. I have practically applied few design patterns myself and would like to share my knowledge with you.

To start off GOF(Gang of Four) design patterns which are considered as the foundation of design patterns have been categorized into three sections namely creational which involves creation of objects, structural and behavioral patterns. I will cover all three sections by explaining one or more patterns from each individual section.

Creational Section:

Singleton Pattern:

This is a very popular creational pattern which restricts a class to have only one instance.

Example: "Prime Minister of India" is a singleton pattern as he/she has unique responsibilities and attributes.

Implementation: A singleton class can't have a public constructor and it has to be sealed. The entry point to get the singleton instance would be a static method or a static property.

public sealed class PrimeMinister
{
private static PrimeMinister instance = new PrimeMinister();
private PrimeMinister(){ }
public static PrimeMinister Instance
{
get { return instance; }
}
}

 
Abstract Factory Pattern:

Abstract Factory is commonly known as factory pattern. In this pattern all the classes involved implement/realize the same interface and the compiler only knows that the created object implements the specific interface but not the object's type. This is very flexible when you need to make a creation decision at runtime depending on some aspect of object's behavior rather than it's type.

Example: Imagine that there is a rental store of cars, bikes, trucks, etc but you want to deliver a vehicle depending on the customer's choice.

Implementation: Programmatically you can make a run time decision using a switch statement to return a new instance of a class which implements the specific interface depending on the customer's choice. As you can see in the constructor of "Journey" class we are getting vehicletype of the current customer and passing it to the VehicleSupplier class's static method "GetVehicle". VehicleSupplier relies on a switch statement to return the vehicle needed, so if you observe here compiler just knows that newly created object implements the "IVehicle" interface but not if it's a car, truck or a bike upfront.

public class Journey
{
public IVehicle rentedVehicle;
public Journey(string customerID)
{
VehicleType vType = Customer.GetVehicleType(customerID);
/*Here is the late binding. Compiler doesn't know the type of the object except that it implements the IVehicle interface*/
rentedVehicle = VehicleSupplier.GetVehicle(vType);
}
//Method for beginning the journey
public void BeginJourney()
{
if(rentedVehicle != null)
{
rentedVehicle.Drive();
}
}
//Method for parking the vehicle
public void ParkTheVehicle()
{
if(rentedVehicle != null)
{
rentedVehicle.Park();
}
}
}
//The class which returns the new vehicle instance depending on the //vehicle type
public class VehicleSupplier
{
public static IVehicle GetVehicle(VehicleType vType)
{
switch(vType)
{
case VehicleType.CAR:
return new Car();
case VehicleType.TRUCK:
return new Truck();
case VehicleType.BIKE:
return new Bike();
}
return null;
}
}
//The interface which will be implemented by Car, Truck and Bike //classes
public interface IVehicle
{
void Drive();
void Park();
}
//enum of the vehicle types
public enum VehicleType{CAR=1,TRUCK,BIKE};

Structural Section:

Structural patterns depend on the structure of the classes involved. There are many structural patterns like Adapter, Builder, Decorator, etc. I will explain adapter pattern with an example here.

Adapter Pattern:

By converting the interface of one class into another interface which is expected by the clients we can make incompatible classes work together and this is the adapter pattern.

Example: Consider that you bought a gadget which expects 110v power supply but you are getting 240v supply, you need to get a power adapter in this case for converting 110v to 240v. This is a perfect example for the adapter pattern. In the software context you may need to have adapters for converting data from legacy systems like AS400 to XML or some other format for export purposes.

Implementation: As you can see the "StepUpStepDownPowerAdapter" object is used for converting power supply of 240V to 110V in the "TestUSGadget" class. The "USGadget" class defines the required voltage of the gadget and other properties. The converted power supply is given to the instance of "USGadget".

/// Class for testing the gadget
public class TestUSGadget
{
public static void Test()
{
USGadget gadget =
new USGadget();
//Indian or European power supply
PowerSupply supply = new PowerSupply(VoltageTypes.V240);
if(gadget.ExpectedVoltage != supply.Voltage)
{
StepUpStepDownPowerAdapter adapter =
new StepUpStepDownPowerAdapter();
//Getting the converted power
supply = adapter.Convert(supply,gadget.ExpectedVoltage);
gadget.Power = supply;
gadget.Start();
}
}
}
public class StepUpStepDownPowerAdapter
{
public StepUpStepDownPowerAdapter()
{
}
//Method for coverting voltages
public PowerSupply Convert(PowerSupply supply,VoltageTypes convertToVoltage)
{
if(supply == null) return supply;
//Convert iff the voltage is not in expected way
if(supply.Voltage != convertToVoltage)
supply.Voltage = convertToVoltage;
return supply;
}
}
/// The power supply class
public class PowerSupply
{
VoltageTypes voltageType;
//There will be other properties as well
public PowerSupply(VoltageTypes vType)
{
voltageType = vType;
}
public VoltageTypes Voltage
{
get
{
return voltageType;
}
set
{
voltageType =
value;
}
}
}
//Voltage Types enum
public enum VoltageTypes{V110,V240};
//Gadget which expects 110V
public class USGadget
{
VoltageTypes reqVoltage;
PowerSupply supply;
public USGadget()
{
reqVoltage = VoltageTypes.V110;
}
public VoltageTypes ExpectedVoltage
{
get
{
return reqVoltage;
}
}
public PowerSupply Power
{
get
{
return supply;
}
set
{
supply =
value;
}
}
public void Start()
{
}
}

Behavioral Section:

Behavioral Patterns define the behavior of the classes involved. The popular behavioral patterns include Chain of Responsibilities,Interpreter, Mediator, Iterator, Observer, etc. I will explain Observer pattern here.

Observer pattern involves a one-many dependency between objects where a change in an object(subject) needs to be notified to all it's dependents(observers).

Example: Consider a scenario where a job posting at some company got multiple applications. Whenever the job status changes (filled, removed or suspended) all the applicants of the job should be notified. In this case job object is subject and all the applicants are observers.

Implementation: As you can see below "Job" is the subject class and all applicants of that particular job are observers. Job class has "Add" and "Remove" methods for adding and removing applicants to it's list. Whenever job status changes all the applicant objects would be notified through Notify method which in turn calls the "Update" method of the applicant object.

/// This the subject in the observer pattern.
public class Job
{
private ArrayList applicants;
private JobStatus statusOfJob;
public Job()
{
applicants =
new ArrayList();
}
public void Add(Applicant candidate)
{
applicants.Add(candidate);
}
public void Remove(Applicant candidate)
{
applicants.Remove(candidate);
}
public void Notify()
{
foreach (Applicant candidate in applicants)
{
candidate.Update(
this);
}
}
public JobStatus Status
{
get
{
return statusOfJob;
}
set
{
statusOfJob =
value;
Notify();
}
}
}
//Jobstatus enumerator
public enum JobStatus{FILLED,SUSPENDED,REMOVED};
/// This is Observer.
public class Applicant
{
//declare variables
string fname;
string lname;
string emailID;
string phoneNo;
public Applicant()
{
//
// TODO: Add constructor logic here
//
}
#region Properties for exposing the member variables
#endregion
public void Update(Job appliedJob)
{
switch(appliedJob.Status)
{
case JobStatus.FILLED:
//Do something like sending email, //updating database, etc
break;
case JobStatus.REMOVED:
//Do something like sending email, //updating database, etc
break;
case JobStatus.SUSPENDED:
//Do something like sending email, //updating database, etc
break;
}
//Your own functionality
//End Of Functionality
}
}

Conclusion

All in all design patterns are really helpful in solving design issues. Having the knowledge of various design patterns will make things much simpler whenever you come across such design problems.


Login to add your contents and source code to this article
 About the author
 
Maruthi Thenneru
Maruthi Thenneru is an MCSD in .NET and developing enterprise level B2B web solutions is one of his passions. He is been developing applications in .NET environment for the last three and half years.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
PatternsCode.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.