Pattern Name:

Adapter Pattern

Short Description:

Match interfaces of classes with different interfaces

Usage:

Often used and easy to implement, useful if classes need to work together that have incompatible existing interfaces.

Complexity:

1 / 5

UML Class Diagram:

image

Explanation:

  • The TradingDataImporter class acts as a client using classes with an existing Connector interface.
    public class TradingDataImporter
    {
        public void ImportData(Connector connector)
        {
            connector.GetData();
        }
    }
  • The abstract Adapter class defines the interface that the client class knows and that it can work with.
  • The concrete Adapter classes convert the interface of the incompatible classes into an interface the client expects. They make different existing interfaces work together.
    public abstract class Connector
    {
        public abstract void GetData();
    }

    public class DatabaseConnector : Connector
    {
        public override void GetData()
        {
            var databaseHelper = new DatabaseHelper();
            databaseHelper.QueryForChanges();
        }
    }

    public class XmlFileConnector : Connector
    {
        public override void GetData()
        {
            var xmlfileLoader = new XmlFileLoader();
            xmlfileLoader.LoadXML();
        }
    }

    public class HttpStreamConnector : Connector
    {
        public override void GetData()
        {
            var websiteScanner = new WebSiteScanner();
            websiteScanner.Scan();
        }
    }
  • Here are some examples of different adaptee classes that implement different interfaces. However, the client expects a generic interface that they currently don’t provide. That is why they get wrapped by the concrete adapter classes to make them compatible with the client.
    public class DatabaseHelper
    {
        public void QueryForChanges()
        {
            Console.WriteLine("Database queried.");
        }
    }

    public class WebSiteScanner
    {
        public void Scan()
        {
            Console.WriteLine("Web sites scanned.");
        }
    }

    public class XmlFileLoader
    {
        public void LoadXML()
        {
            Console.WriteLine("Xml files loaded.");
        }
    }
  • In the last step we add some code to test the software design and the Adapter implementation.
    public static void Adapter()
    {
        var tradingdataImporter = new TradingDataImporter();

        Connector databaseConnector = new DatabaseConnector();
        tradingdataImporter.ImportData(databaseConnector);

        Connector xmlfileConnector = new XmlFileConnector();
        tradingdataImporter.ImportData(xmlfileConnector);

        Connector httpstreamConnector = new HttpStreamConnector();
        tradingdataImporter.ImportData(httpstreamConnector);           

        Console.ReadKey();
    }
  • When running the example you can see that everything is working as expected (it is however not as simple to test that there really is just one single instance, you just have to believe in the correct software design).

image_thumb[6]

Last edited Jul 1, 2011 at 12:47 PM by JasonOliveira, version 2

Comments

No comments yet.