介面卡設計模式

**** 顧名思義, 介面卡 是允許兩個互不相容的介面相互通訊的物件。

例如: 如果你購買 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 的最佳示例。