Geeks With Blogs

News Currently working on complete Enterprise Design Patterns set of examples and blog articles based on AdventureWorks 2008 R2 Sample Database. This is my favorite sample database as you can use if for about anything from simple applications development to business intelligence applications
The Ron on .NET Software Architecture Ron Garlit's thoughts and informational blogs on Microsoft .NET

“Design Patterns 100″ is a prerequisite for .NET Developers.

(Part 3 – Excerpts from July 2010 – PhillyNJ.NET Presentation)

Continuing our discussion from Part-2 we ask.

What are the “Gang of Four” (GoF) Creational Patterns and where can we find them in the .NET Framework?

Let’s look at a list of the Creational Patterns as defined by the GoF.

First we have the ”Abstract Factory” and “Factory Method” which I’ll discuss together for a couple reasons.

Since the release of .NET Framework 2.0 the naming convention in .NET for the Factory pattern is to append the word ‘Factory’ to the name of the type that is being created.

Adding to the confusion or rather the discussion is that these two GoF patterns in the .NET Framework can vary greatly from what is imagined based on the GoF definition to what has been implmented.

The Abstract Factory pattern is essentially a generalization of the Factory Method as it creates families of related classes that interact with each other in predictable ways.

Over time the differences these two factory patterns have become blurry and developers usually just mention the Factory pattern, meaning a class that manufactures objects that share a common interface or base class.

By definition the Abstract Factory creates an instance of several families of classes

If you go to the Object Browser in Visual Studio and do a search through the .NET Framework libraries for the word ‘Factory’ it reveals numerous classes that are implementations of the Factory design pattern.

Looking at ADO.NET, for example, includes two Abstract Factory classes that offer provider independent data access.

They are: DbProviderFactory and DbProviderFactories. The DbProviderFactory creates database specific classes you need; in the case of SQL Server these are SqlClientConnection, SqlClientCommand, and SqlClientDataAdapter. There are other managed providers such as, SqlClient, OleDb, ODBC, or Oracle that each have their own DbProviderFactory class.

DbProviderFactory objects are created by the DbProviderFactories classes, which are themselves each a factory. In fact, it is a factory of factories — it manufactures different factories, one for each provider.

For the record the GoF define a Factory Method as one that creates an instance of several derived classes

The Factory Method is frequently used in .NET. One example is the System.Convert class which exposes many static methods that, given an instance of a type, returns another new type. Convert.ToBoolean accepts a string and returns a boolean with value true or false depending on the string value.

Whereas the Parse method on many built-in value types like Int32 and Double are examples of the Factory Method pattern.

GoF says that the Builder Pattern “Separates object construction from its representation”

In .the .NET Framework the Builder design pattern isn’t used often, but you can still find it. The two .NET classes are VBCodeProvider and CSharpCodeProvider, which create Builder classes through their CreateGenerator methods. As a side note both of these are factory classes. Visual Studio .NET uses these code generating Builder classes internally.

The Prototype Pattern creates fully initialized instance that is to be copied or cloned from and example.

In the .NET Framework support for the Prototype pattern can be found in object serialization scenarios. Let’s say you have a object that has been serialized to persistent storage, such as, disk or a database. Having this serialized representation as a prototype you can then use it to create copies of the original object.

The Singleton Pattern ensures that a class only has single instance while providing global access.

The.NET Framework uses the Singleton pattern with .NET Remoting when it launches server-activated objects. One of the activation modes of server objects is called Singleton and their behavior is in line with the GoF pattern definition, meaning that there is never more than one instance at any one time.

(NEXT: Design Patterns 100-Part4 – Structural Patterns)

Posted on Tuesday, May 24, 2011 9:04 PM | Back to top

Comments on this post: Design Patterns 100-Part3

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © RonGarlit | Powered by: