Design Pattern Tutorials

Adapter Design Pattern

Adapter Design Pattern : The Adapter design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. As per the GOF definition Adapter "Match interfaces of different classes". This means, an adapter allows two incompatible interfaces to work together
  • In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface. It is often used to make existing classes work with others without modifying their source code.
  • Leveraging on Adapter pattern Improves reusability of older functionality 
Implementation Guidelines : We choose Adapter Design Pattern when
  • A class need to be reused that does not have an interface that a client requires?
  • We need to work through a separate Adapter that adapts the interface of an  existing class without changing it
  • Clients don't know whether they work with a Target class directly or through an Adapter with a class that does not have the Target interface
Example 
  • The best example for the adapter pattern is based around AC power adapter
  • The AC adapter knows how to deal with both sides, acting as a middleman - this behavior of Ac Power Adapter is the perfect example of adapter pattern
Let’s now take a look at the representation diagram of Adapter design pattern.
adapter design pattern implementation

Client class is used to communicate with the Adapter class that implements the ITarget interface. In other words, this is the class used for creating the instance of Adapter class.

ITarget here represents an interface created to make client achieve its purpose.

The Adapter class implements the ITarget interface and inherits the Adaptee class as well. 

Adaptee class contains the main functionality that client is expecting.

Adapter Implementation

Step 1 : Create a console program and add Employee and EmployeeManager classes.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace AdapterDesignPattern
{
    [Serializable]
    public class Employee
    {
        Employee() { }
        public Employee(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
        [XmlAttribute]
        public int ID { get; set; }
        [XmlAttribute]
        public string Name { get; set; }
    }

    public class EmployeeManager
    {
        public List<Employee> employees;
        public EmployeeManager()
        {
            employees = new List<Employee>();
            this.employees.Add(new Employee(1, "John"));
            this.employees.Add(new Employee(2, "Michael"));
            this.employees.Add(new Employee(3, "Jason"));
        }
        public virtual string GetAllEmployees()
        {
            var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
            var serializer = new XmlSerializer(employees.GetType());
            var settings = new XmlWriterSettings();settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            using (var stream = new StringWriter())
            using (var writer = XmlWriter.Create(stream, settings))
            {
                serializer.Serialize(writer, employees, emptyNamepsaces);
                return stream.ToString();
            }           
        }
    }
}

Step 2 : Add IEmployee Interface to Target folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdapterDesignPattern
{
    public interface IEmployee
    {
        string GetAllEmployees();
    }
}

Step 3 : Add Employee Adapter class and inherit IEmployee and EmployeeManager Classes
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace AdapterDesignPattern.Adapter
{
    public class EmployeeAdapter : EmployeeManager, IEmployee
    {
        public override string GetAllEmployees()
        {
            string returnXml = base.GetAllEmployees();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(returnXml);
            return JsonConvert.SerializeObject(doc, Newtonsoft.Json.Formatting.Indented);
        }
    }
}

Step 4 : Invoke the EmployeeAdapter class in the main program which is our client. Run the application and notice that the xmlString is converted to JsonString with The Adapter Implementation.
using AdapterDesignPattern.Adapter;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace AdapterDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            IEmployee emp = new EmployeeAdapter();
            string value = emp.GetAllEmployees();
            Console.ReadLine();
        }
    }
}

;