Wednesday 19 December 2007

Session 1

Following is the summary and some applications of today's tech nirvana session.

Design Patterns [Application Design Skill]
- Factory [Creational] - Abstracts object creation
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
- Strategy[Behavioral]
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it

Applications of Factory pattern in .NET Framework
(1) System.Convert class
The System.Convert class is a good example of factory pattern.
For e.g. Convert.ToBoolean(int) - returns new Boolean set to true if the int was non-zero, false otherwise.

(2) System.Net.WebRequest
Similarly the System.Net.WebRequest class also is an excellent example of factory pattern.
- Pass in the URI to the Create method and it creates respective subclass i.e. HttpWebRequest, FtpWebRequest or FileWebRequest.

Applications of Strategy pattern in .NET Framework
(1) Array and ArrayList provides capability to sort object. By default they use QuickSort algorithm. The Sort method will use the IComparable implementation for each
element to handle the comparisons necessary for sorrting. To change the sortig strategy use the overload of Sort that takes an IComparer as a parameter.
ICompararer.Compare is then used for the comparisons.

The new List makes heavy use of the strategy pattern. In addition to the pdated Sort method, the find-related methods, BinarySearch and others all take
parameters that allow parts of the respective algorithms to vary based on the needs of the caller. The use of Predicate delegate in the FindAll method lets the
caller use any method as a filter for the List so long as it takes the appropriate object type and returns a boolean.

Terms introduced:
- OCP (Open Closed Principle).
Classes should be open for extension but closed for modification.

"Hope this nicely conjures up everything we discussed."

3 comments:

Unknown said...

Abstract factory design pattern: DbProviderFactories.GetFactory()[System.Data.Common]

DevRaj said...

Factory design pattern: EncodedStreamFactory [System.Net.Mime]

DevRaj said...

Design pattern in .Net :

1) Singleton: SqlClientFactory [System.Data.SqlClient]
2) Template: Page Class (System.web.UI), ToString() of Object Class
3) Null pattern: : EmptyResult [System.Web.Mvc]