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
|
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.
|
|