Sök…


Introduktion

Strukturella designmönster är mönster som beskriver hur objekt och klasser kan kombineras och bildar en stor struktur och som underlättar design genom att identifiera ett enkelt sätt att förverkliga relationer mellan enheter. Det finns sju strukturella mönster som beskrivs. De är som följer: Adapter, Bridge, Composite, Decorator, Facade, Flyweight och Proxy

Adapter Design Mönster

"Adapter" som namnet antyder är objektet som låter två ömsesidigt inkompatibla gränssnitt kommunicera med varandra.

Till exempel: om du köper en Iphone 8 (eller någon annan Apple-produkt) behöver du många adapter. Eftersom standardgränssnittet inte stöder ljudjac eller USB. Med dessa adaptrar kan du använda hörlurar med kablar eller du kan använda en vanlig Ethernet-kabel. Så "två ömsesidigt inkompatibla gränssnitt kommunicerar med varandra" .

Så i tekniska termer betyder detta: Konvertera gränssnittet för en klass till ett annat gränssnitt som en klient förväntar sig. Adapter låter klasser fungera tillsammans som annars inte kunde bero på inkompatibla gränssnitt. Klasser och objekt som deltar i detta mönster är:

Adaptermönstret går ut ur fyra element

  1. IT-mål: Detta är gränssnittet som används av klienten för att uppnå funktionalitet.
  2. Adaptee: Detta är den funktionalitet som klienten önskar men dess gränssnitt är inte kompatibelt med klienten.
  3. Klient: Det här är den klass som vill uppnå viss funktionalitet med hjälp av adaptees kod.
  4. Adapter: Detta är klassen som skulle implementera ITarget och ringa Adaptee-koden som klienten vill ringa.

UML

ange bildbeskrivning här

Första koden Exempel (teoretiskt exempel) .

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

Andra kodexempel (Implementering av verklig värld)

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

När du ska använda den

  • Låt ett system använda klasser i ett annat system som inte är kompatibelt med det.
  • Tillåt kommunikation mellan nya och redan befintliga system som är oberoende av varandra
  • Ado.Net SqlAdapter, OracleAdapter, MySqlAdapter är bäst exempel på Adaptermönster.


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow