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 5 – Excerpts from July 2010 – PhillyNJ.NET Presentation)

Continuing our discussion from Part-4 we ask.

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

Chain of Responsibility is a way of passing a request between a chain of objects

You can identify a Chain of Responsibility in the Windows event model where each UI control can decide to process an event or let it fall through to the next control in
the event chain.
Occasionally you may run into a Chain of Responsibility implementation in which a chain of objects process a message between a sender and a receiver, in which each object does some processing on the message as it travels through the chain from the sender to the receiver. This is slightly different from the GoF definition in which just one object in a chain decides to handle the request.

The .NET Framework implements this ‘stepwise chain pattern’ in .NET Remoting in which a message between a client and a server passes through one or more so-called message sinks. Message sinks form a chain as each sink has a reference to the next sink in the chain. These sinks implement the IMessageSink interface and one of its members is the NextSink property.

Command Encapsulates a command request as an object

Command in the .NET Framework is hidden in the the source code we can’t see. But I’m fairly sure that many Microsoft’s applications, including Visual Studio .NET, uses the Command pattern to support their menus, toolbars, shortcuts, and associated undo functionality.

We would have expected that the Command pattern would be exposed in .NET as part of WinForms architecture, but they are not. Until recently the Command patterns was not generally used in the .NET Framework. The Introduction of WPF changed that as WPF natively supports Commands in its Command System.

Interpreter is a way to include language elements in a program

I’m not really aware of the Interpreter pattern being used in the .NET Framework libraries. But is valuable for building scripting parsers, but it has limited value to business type applications. This is a highly specialized design pattern.

Iterator Sequentially accesses the elements of a collection

Iterator in the .NET Framework is not only part of the .NET Framework libraries, it is baked into the language itself. The .NET libraries contain numerous classes that implement the IEnumerable and IEnumerator interfaces, such as, Array, ArrayList, AttributesCollection, BaseChannelObjectWithProperties, BaseCollection, BindingsContext, as well as the generic counterparts of these classes.

.NET 3.0 made iteration even more fun and powerful with LINQ (language integrated query) which is a query language built on top of the generic IEnumerable<T> and IEnumerator<T> iterator types.

The foreach (For Each in VB) language construct is an implementation of the Iterator pattern. In fact, LINQ is almost entirely designed around the Iterator pattern.

Mediator Defines simple communication between classes

Mediator in the .NET Framework is not used in the .NET Framework libraries.

Memento Capture and restore and object’s internal state

Memento in the .NET Framework Serialization is the process of converting an object into a linear sequence of bytes for either storage or transmission to another location.

Deserialization is the process of taking in stored information and recreating objects from it. The Memento pattern creates a snapshot of an object’s state and then offers the ability to restore it to its original state.

This is what the serialization architecture of .NET offers and therefore qualifies as an example of Memento in the .NET Framework.

Observer A way of notifying change to a number of classes

Observer in the .NET Framework As mentioned, the .NET event model is implemented with the Observer design pattern and is found throughout the .NET Framework — both in the .NET languages and .NET class libraries.

.NET events and delegates are an implementation of the Observer pattern.

State Alter an object’s behavior when its state changes

State in the .NET Framework is not used in the .NET Framework it self.

The State Pattern, for example, is used when you have clearly defined state transitions, such as a Credit card application process that goes through a sequence of steps.

Strategy encapsulates an algorithm inside a class

An example of the Strategy pattern in .NET is the ArrayList which contains several overloaded Sort methods. These methods sort the elements in the list using a given class that implements the IComparer interface.

IComparer contains a Sort method that compares two objects and returns a value indicating whether one object is greater than, equal to, or less than the other object. Classes that implement the IComparer interface are implementations of the Strategy design pattern.

Template Method Defer the exact steps of an algorithm to a subclass

The Template Method is frequently used in the .NET Framework. It is through this pattern that .NET provides extensibility to the users of its API.

Take a look at custom controls in ASP.NET. Custom controls are created by inheriting from one of the control base classes (Control or WebControl). Also the Control class in the System.Windows.Forms namespace demonstrates usage of the Template Method.

These template methods allow the base class designer controlled extensibility by centralizing these methods as a single virtual method.

Microsoft suffixes these methods with the word “Core”.

Visitor defines a new operation to a class without change

The Visitor pattern is not used in the .NET Framework libraries.

If you discover anymore examples of the base patterns in the .NET Framework please leave a comment on this blog.

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


Comments on this post: Design Patterns 100–Part5

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


Copyright © RonGarlit | Powered by: GeeksWithBlogs.net