适配器设计模式

**** 顾名思义, 适配器 是允许两个互不兼容的接口相互通信的对象。

例如: 如果你购买 Iphone 8(或任何其他 Apple 产品),你需要很多适配器。因为默认接口不支持音频 jac 或 USB。使用这些适配器,你可以使用带有电线的耳机,也可以使用普通的以太网电缆。因此 两个互不兼容的接口相互通信

因此,在技术术语中,这意味着: 将类的接口转换为客户期望的另一个接口。适配器让类一起工作,否则由于不兼容的接口。参与此模式的类和对象是:

适配器模式退出 4 个元素

  1. ITarget: 这是客户端用来实现功能的接口。
  2. Adaptee: 这是客户所需的功能,但其界面与客户端不兼容。
  3. 客户端: 这是希望通过使用适配器代码实现某些功能的类。
  4. 适配器: 这是实现 ITarget 并将调用客户端想要调用的 Adaptee 代码的类。

UML

https://i.stack.imgur.com/oYMFy.gif

第一个代码示例(理论示例)

public interface ITarget
{
    void MethodA();
}

public class Adaptee
{
    public void MethodB()
    {
        Console.WriteLine("MethodB() is called");
    }
}

public class Client
{
    private ITarget target;

    public Client(ITarget target)
    {
        this.target = target;
    }

    public void MakeRequest()
    {
        target.MethodA();
    }
}  

public class Adapter : Adaptee, ITarget
{
    public void MethodA()
    {
        MethodB();
    }
}

第二个代码示例(真实世界的实现)

/// <summary>
///  Interface: This is the interface which is used by the client to achieve functionality.
/// </summary>
public interface ITarget
{
    List<string> GetEmployeeList();
}

/// <summary>
/// Adaptee: This is the functionality which the client desires but its interface is not compatible with the client.
/// </summary>
public class CompanyEmplyees
{
    public string[][] GetEmployees()
    {
        string[][] employees = new string[4][];

        employees[0] = new string[] { "100", "Deepak", "Team Leader" };
        employees[1] = new string[] { "101", "Rohit", "Developer" };
        employees[2] = new string[] { "102", "Gautam", "Developer" };
        employees[3] = new string[] { "103", "Dev", "Tester" };

        return employees;
    }
}

/// <summary>
/// Client: This is the class which wants to achieve some functionality by using the adaptee’s code (list of employees).
/// </summary>
public class ThirdPartyBillingSystem
{
    /* 
     * This class is from a thirt party and you do'n have any control over it. 
     * But it requires a Emplyee list to do its work
     */

    private ITarget employeeSource;

    public ThirdPartyBillingSystem(ITarget employeeSource)
    {
        this.employeeSource = employeeSource;
    }

    public void ShowEmployeeList()
    {
        // call the clietn list in the interface
        List<string> employee = employeeSource.GetEmployeeList();

        Console.WriteLine("######### Employee List ##########");
        foreach (var item in employee)
        {
            Console.Write(item);
        }

    }
}

/// <summary>
/// Adapter: This is the class which would implement ITarget and would call the Adaptee code which the client wants to call.
/// </summary>
public class EmployeeAdapter : CompanyEmplyees, ITarget
{
    public List<string> GetEmployeeList()
    {
        List<string> employeeList = new List<string>();
        string[][] employees = GetEmployees();
        foreach (string[] employee in employees)
        {
            employeeList.Add(employee[0]);
            employeeList.Add(",");
            employeeList.Add(employee[1]);
            employeeList.Add(",");
            employeeList.Add(employee[2]);
            employeeList.Add("\n");
        }

        return employeeList;
    }
}

/// 
/// Demo
/// 
class Programs
{
    static void Main(string[] args)
    {
        ITarget Itarget = new EmployeeAdapter();
        ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget);
        client.ShowEmployeeList();
        Console.ReadKey();
    }
}

什么时候用

  • 允许系统使用与其不兼容的其他系统的类。
  • 允许新系统和现有系统之间的通信彼此独立
  • Ado.Net SqlAdapter,OracleAdapter,MySqlAdapter 是 Adapter Pattern 的最佳示例。