構造設計パターン

構造設計パターンは、オブジェクトとクラスを組み合わせて大きな構造を形成する方法を記述し、エンティティ間の関係を実現する簡単な方法を特定することで設計を容易にするパターンです。 7 つの構造パターンが記述されています。それらは次のとおりです:アダプター、ブリッジ、コンポジット、デコレーター、ファサード、フライウェイト、およびプロキシー

# アダプターの設計パターン

[**「アダプター」**](https://en.wikipedia.org/wiki/Adapter_pattern) は、その名前が示すように、相互に互換性のない 2 つのインターフェイスが相互に通信できるようにするオブジェクトです。

**例:** Iphone 8 (またはその他の Apple 製品) を購入した場合、多くのアダプターが必要になります。デフォルトのインターフェイスはオーディオ ジャックまたは USB をサポートしていないためです。これらのアダプターを使用すると、有線のイヤホンを使用したり、通常のイーサネット ケーブルを使用したりできます。したがって、**「相互に互換性のない 2 つのインターフェースが互いに通信する」**.

**つまり、技術的には次のことを意味します:** aclass のインターフェイスを、クライアントが期待する別のインターフェイスに変換します。互換性のないインターフェイスのために他の方法ではできなかったアダプターの letclass が連携します。このパターンに参加するクラスとオブジェクトは次のとおりです:

アダプター パターンは 4 つの要素を終了します

    - **ITarget:** これはクライアントが機能を実現するために使用するインターフェースです.- **Adaptee:** これはクライアントが望む機能ですが、そのインターフェースはクライアントと互換性がありません.- **クライアント:** これは、アダプティーのコードを使用して何らかの機能を達成したいクラスです。- **アダプター:** これは、ITarget を実装し、クライアントが呼び出したいアダプティー コードを呼び出すクラスです。

UML

最初のコード例 (理論上の例) .

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();
    }
}

2 番目のコード例 (実際の実装)

/// <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 パターンの最良の例です。