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
parameters that allow parts of the respective algorithms to vary based on the needs of the caller. The use of Predicate
caller use any method as a filter for the List
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:
Abstract factory design pattern: DbProviderFactories.GetFactory()[System.Data.Common]
Factory design pattern: EncodedStreamFactory [System.Net.Mime]
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]
Post a Comment