C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Two Common Design Patterns


Posted Date: 20 Jan 2006    Resource Type: Articles    Category: General
Author: Chakravarthy RMember Level: Bronze    
Rating: Points: 5



Introduction



‘Design Patterns’ are a way of using Object Oriented techniques to meet common programming challenges . The ‘Design Patterns’ were described in a book called ‘Design Patterns – Elements of Reusable Object-Oriented Software’ by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, referred to as the ‘Gang of Four’ , or ‘GoF’. John Vlissides passed away on 24th November 2005.

Singleton Pattern



Suppose we wanted a particular class to be instantiated only once at a time, we could use this pattern. The classic example is the way a MSN messenger or Yahoo messenger can be instantiated, or run, only once at a time for a given account. This would be an ideal candidate for the Singleton Pattern.

The way this pattern is used in OOPS style, is to make the constructor for the class a private method. There is then a static method which invokes the constructor from within the class , after confirming that the class has not already been instantiated (a static flag is set once the class has been instantiated). The object is created by calling the static class which invokes the constructor, and not by the typical ‘new’ operator.

This pattern can be used to create an ADO.NET connection, and return the connection object to various callers in an application. Since this class will create the ADO.NET connection object only once, it ensures that the ADO.NET object is created and returned only once , which can then be re-used by all the users in that application.


Observer Pattern


This pattern reflects a situation where there are many objects ‘observing’ or watching a particular ‘Subject’. The ‘Subject’ will be the class whose event triggers these observers to react accordingly. The event handlers in a windows form, the ‘one-click’ deployment of .NET 2.0 , and the ‘You have mail’ alert are typical examples of the usage of the ‘Observer Pattern’.

The observer pattern applies to situations where the ‘subject’ pushes the message regarding the happening of the event (like the receipt of mail) to it’s subscribers, as against a ‘pull’ mechanism where the client 'sleeps' for sometime and then goes and checks the ‘subject’ if the desired event has occurred.


The way this is implemented in OO programs is to register the ‘event handlers’ (or subscribers) to the ‘subject’ (or event). A typical example is the way a .NET windows forms attaches one or more event handlers to an event.




Summary



Learning design patterns help us to use commonly applied techniques for a given class of problems . This helps us to re-use techniques , which is another way of optimizing the development.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Top 10 Technology Trends in 2006
Previous Resource: Implementation Patterns
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use