Home > Development > Putting a Square Peg in a Round Hole – Adapter Pattern C#

Putting a Square Peg in a Round Hole – Adapter Pattern C#


Hi all,
Today I am going to go through what I believe is an especially useful design pattern. I use this heaps, especially when I have a third party API and I need to convert my code into its format.

Here is the definition from the website do factory: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Here is the task, consider the following code:

using System;

namespace AdapterPattern
{

    public class SquarePeg
    {
        public virtual void Print()
        {
            Console.WriteLine("Im a Square Peg...");
        }
    }

    public class RoundPeg
    {
        public virtual void Print()
        {
            Console.WriteLine("Im a Round Peg...");
        }
    }

    public class RoundPegProcessor
    {
        public void Process(RoundPeg peg)
        {
            peg.Print();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

So there issue is how to pag a SquarePeg to the process method. Well you cant. Or Can you 🙂
Actually we can what we need is an adapter class, A class that wraps a square peg but looks like a round peg.

Lets look at the solution.

using System;

namespace AdapterPattern
{

    public class SquarePeg
    {
        public virtual void Print()
        {
            Console.WriteLine("Im a Square Peg...");
        }
    }

    public class RoundPeg
    {
        public virtual void Print()
        {
            Console.WriteLine("Im a Round Peg...");
        }
    }

    public class SquarePegAdapter : RoundPeg
    {
        private readonly SquarePeg _peg;

        public SquarePegAdapter(SquarePeg peg)
        {
            _peg = peg;
        }

        public override void Print()
        {
            _peg.Print();
        }
    }

    public class RoundPegProcessor
    {
        public void Process(RoundPeg peg)
        {
            peg.Print();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

No to hard hey. Nice trick to help you out.

Blair….

Advertisements
  1. Phil
    July 25, 2010 at 10:27 am

    Hey Blair,

    a nice example. Although, I couldn’t help but write a similar one that actually puts a square peg in a round hole! Its all very similar. I’ve overridden ToString(), and used a Hole class instead of a processor. In real cases, the adapter classes should really be deriving from an interface (ISquarePeg, or even IPeg?), which of course makes much easier to implement a focused adapter (and also possible! Some methods may not be marked as virtual or abstract in the adapted class).

    class SquarePeg
    {
    public override string ToString()
    {
    return “Square Peg”;
    }
    }

    class RoundPeg
    {
    public override string ToString()
    {
    return “Round Peg”;
    }
    }

    class RoundHole
    {
    private RoundPeg _roundPeg;

    public RoundHole() { }

    public RoundHole(RoundPeg peg)
    {
    _roundPeg = peg;
    }

    public override string ToString()
    {
    return string.Format(“{0} in a Round Hole”, _roundPeg == null ? “No peg” : _roundPeg.ToString());
    }
    }

    class SquarePegToRoundPegAdpater : RoundPeg
    {
    private SquarePeg _peg;

    public SquarePegToRoundPegAdpater(SquarePeg peg)
    {
    _peg = peg;
    }

    public override string ToString()
    {
    return _peg.ToString();
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine(“An Empty round Hole”);
    RoundHole emptyHole = new RoundHole();
    Console.WriteLine(emptyHole);
    Console.WriteLine();

    Console.WriteLine(“Putting a Round Peg into a round Hole”);
    RoundHole hole = new RoundHole(new RoundPeg());
    Console.WriteLine(hole);
    Console.WriteLine();

    Console.WriteLine(“And then… “);
    Console.WriteLine();

    RoundHole newHole = new RoundHole(new SquarePegToRoundPegAdpater(new SquarePeg()));
    Console.WriteLine(newHole);

    Console.ReadLine();
    }
    }

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: