Where can I find a good implementation of Adapter Patterns with good examples in C#?
views:
319answers:
3It'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.
(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.