Design Patterns and C#

 design patterns And C#














Patterns are generally categorized into the following three groups:

Creational Design Patterns
Abstract Factory: This pattern creates instances of classes which belong to several families.
Factory Method: This pattern creates instances of several derived classed
Singleton: Class with only one single possible instance
Builder: This pattern separates object construction from its representation
Prototype: A fully initialized instance to be copied or cloned.

Structural Design Patterns
Adapter: This pattern matches interfaces of different classes.
Bridge: This pattern separates an object’s interface from its implementation
Façade: A single class that represents an entire subsystem.
Flyweight: Minimize memory usage by sharing as much data as possible with similar objects
Proxy: An object representing another object.
Composite: Simple and composite objects tree.
Decorator: Dynamically add responsibilities to objects.


Behavioral Patterns
Iterator: Sequentially access the elements of a collection
Observer: Notify dependent objects of state changes
Mediator: Define simplified communication between classes
Memento: Undo modifications and restore an object to its initial state.
State: Alter an object’s behavior when its state changes
Strategy: Encapsulates an algorithm inside a class.
Template Method: Define an algorithm skeleton and delegate algorithm steps to subclasses so that they may be overridden.
Visitor: Defines a new operation to a class without change.
Chain of Responsibility: A way to passing a request between a chain of objects.
Command: Encapsulate a method call as an object containing all necessary information.


Examples in C#
There are many examples in C# and .NET libraries. For example:

Adapter Pattern

Streams are tools to read/write a sequence of bytes. If you need to read/write strings or characters, you would need to create a Reader/Writer class. Fortunately, all the Reader/Writer classes can be constructed from astream object. So, I think that the Reader/Writer classes are adapters which convert a byte array interface to string/char interface.
Adapter Pattern in the DataAdapter used with various data sources such as OleDB, Sql, and Oracle.
Abstract Factory
ADO.Net is all about Abstract Factory for getting rid of the details of connecting to data sources

Decorator Pattern

When you need encryption or compression, or need to add a buffer on the network stream, you can use Decorator pattern. The bufferedStream, CryptoStream, and DeflateStream are decorators to other streams. They attach additional functionalities to existing streams without changing the interface of the original streams.
The Decorator Pattern is used on the Stream classes:
  • System.IO.Stream
    • System.IO.BufferedStream
    • System.IO.FileStream
    • System.IO.MemoryStream
    • System.Net.Sockets.NetworkStream
    • System.Security.Cryptography.CryptoStream
    •  

Flyweight Pattern

To save space, String class holds a reference to an Intern object. So, if two strings have the same literal, they share the same storage space. It uses "sharing" to support a large number of fine grained objects efficiently so that it is using Flyweight pattern.

Iterator Pattern

Iterator provides a way to access the elements inside an aggregate object. C# has foreach keyword which makesIterator really easy.

Observer Pattern

Events in the .Net Framework are an implementation of the Observer pattern


Singleton
The simplest way to create a singleton is to use a static variable in C#.
 
class Singleton
{
    private static Singleton instance = new Singleton();
    public static Singleton GetInstance()
    {
        return instance;
    }
}

Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options