Did you ever wish for a superhuman power to be impervious to bullets or travel outside your body? How about the superpower to be able to breathe underwater or fly? Or how about a changing the way you look so you can disguise yourself as anyone, or anything? In this series of four articles, we will travel down the C# rabbit hole and see how it is all possible with some wrapper patterns: Proxy, Decorator, and Adapter.
Part IV. The Adapter
In our journey through this series we learned how to manifest superpowers with two wrapper-type GOF design patterns. In part II we looked at the Proxy pattern to reveal protection powers. In part III we looked at the Decorator to add functional powers. In this final section we are going to look at the Adapter pattern to see how to implement morphing powers.
Let's start with an INormalDude contract...
public interface INormalDude
{
string Name { get; set; }
void Eat();
void Talk();
}
...and the NormalDude class:
public class NormalDude : INormalDude
{
private string m_name;
private string m_thingToSay;
public string ThingToSay
{
get { return m_thingToSay; }
set { m_thingToSay = value; }
}
#region INormalDude Members
public string Name
{
get { return m_name; }
set { m_name = value; }
}
public void Eat()
{
Console.WriteLine(string.Format("{0}: Please pass the mayo.", m_name));
}
public void Talk()
{
Console.WriteLine(string.Format("{0}: {1}", m_name, m_thingToSay));
}
#endregion
}
Using our C# wrapping powers we will give our NormalDude the superpower to morph so that wolves think that he is one of them.
Here is our IWolf contract...
public interface IWolf
{
string FoodSource { get; set; }
void Devour();
void Howl();
}
...and the Wolf class:
public class Wolf : IWolf
{
private string m_foodSource;
#region IWolf Members
public string FoodSource
{
get { return m_foodSource; }

FelipeeditedPosted May 23, 2007, 11:30 AMEdited May 23, 2007, 11:31 AM
Where's the code for that last result window?