views:

319

answers:

3

Where can I find a good implementation of Adapter Patterns with good examples in C#?

+1  A: 

There's a great podcast on DimeCasts.net about this here

It is a 10 minute video soley on the Adapter pattern, and they also publish the source code so you can look at it as well.

Joseph
+1  A: 

It's an interface conversion pattern. Data & Object Factory: Adapter Pattern. Explanation, UML, example source -- you can also buy their additional source code on their patterns.

JP Alioto
Would negative voter care to explain why?
JP Alioto
+3  A: 

(I'm going to throw in something similar to the Wikipedia sample here...)

Say you had a requirement to provide an IDeque<T> interface for some library, with the following signature:

public interface IDeque<T>
{
    void PushFront(T element);
    T PopFront();
    void PushBack(T element);
    T PopBack();
    int Count { get; }
}

You could implement this easily using a class in the BCL - LinkedList<T>, but the specific interface required here would not match. In order to implement this interface, you'd have to provide an Adapter - a class which fulfilled the required interface, using some other non-compatible interface. This would look something like:

public class Deque<T> : IDeque<T>
{
    LinkedList<T> list = new LinkedList<T>();
    public void PushFront(T element)
    {
        list.AddFirst(element);
    }

    public T PopFront()
    {
        T result = list.First.Value;
        list.RemoveFirst();
        return result;
    }
    // ... Fill in the rest...

In this case, you're just using an existing class (LinkedList<T>), but you're wrapping it in an Adapter in order to make it fulfil a different interface.

Reed Copsey