Patterns. Examples of implementation of the Adapter pattern in C#

Examples of implementation of the Adapter pattern in C#

Before studying this topic, it is recommended that you familiarize yourself with the following topic:


Contents


Search other websites:




1. Structure of the Adapter pattern

The Adapter pattern has two features, which differ in implementation and results:

  • adapter for class (Figure 1). In this case, the inheritance mechanism is used;
  • adapter for object (Figure 2). In this case, object composition is used.

 

Adapter for class

Figure 1. Adapter for class

Adapter for object

Figure 2. Adapter for object

  ⇑

2. The Adapter pattern for the class.

Below is an implementation of the Adapter pattern for a class, which contains the same names as shown in Figure 1.

// The Adapter pattern for the class. Solution in C#
using System;
using static System.Console;

namespace ConsoleApp10
{
  // Adapts the Adaptee class to the ITarget interface through the Adapter intermediate class
  // The ITarget interface
  interface ITarget
  {
    public void Request();
  }

  // The class whose method needs to be adapted to another system.
  // In this case, adapting the name of the SpecificRequest() method to the Request() method
  class Adaptee
  {
    // Some specific method
    public void SpecificRequest()
    {
      WriteLine("Adaptee.SpecificRequest()");
    }
  }

  // Class Adapter - implements the ITarget interface and inherits the Adapter class
  class Adapter : Adaptee, ITarget
  {
    // Implementation of method Request() of interface ITarget
    public void Request()
    {
      // In the middle of the method, the Specific Request() method of the Adapter() class is called
      SpecificRequest();
    }
  }

  // Class Client - gets a reference to ITarget interface
  class Client
  {
    // some method of class Client
    public void ClientMethod(ITarget target)
    {
      target.Request();
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      // Demonstration of the Adapter pattern for a class.
      // Task.
      // You need to adapt the Adaptee instance to the needs of the Client instance.
      // 1. Create the instance of Client class
      Client client = new Client();

      // 2. Create an instance of the Adapter class that references the ITarget interface
      ITarget target = new Adapter();

      // 3. Pass target to the client
      client.ClientMethod(target);
    }
  }
}

  ⇑

3. Adapter for object

The following code demonstrates the implementation of the Adapter pattern for an object from Figure 2.

// The Adapter pattern for the object. Solution in C#
using System;
using static System.Console;

namespace ConsoleApp10
{
  // Adapts the Adaptee class to the Target interface via the Adapter intermediate class
  // Interface ITarget
  interface ITarget
  {
    public void Request();
  }

  // The class whose method needs to be adapted to another system.
  // In this case, adapt the name of the SpecificRequest() method to the Request() method
  class Adaptee
  {
    // Some specified method
    public void SpecificRequest()
    {
      WriteLine("Adaptee.SpecificRequest()");
    }
  }

  // Class Adapter - implements the ITarget interface
  class Adapter : ITarget
  {
    private Adaptee adaptee; // reference to Adaptee

    // Constructor
    public Adapter(Adaptee adaptee)
    {
      // initialize internal reference adaptee with parameter
      this.adaptee = adaptee;
    }

    // Implementation of Request() method of ITarget interface
    public void Request()
    {
      // In the middle of the method, the Specific Request() method of the Adapter() class is called
      adaptee.SpecificRequest();
    }
  }

  // Class Client - receives a reference to ITarget interface
  class Client
  {
    // some method of class Client
    public void ClientMethod(ITarget target)
    {
      target.Request();
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      // Demonstration of the Adapter pattern for an object.
      // Task.
      // You need to adapt the Adaptee instance to the needs of the Client instance.
      // 1. Let some instances of Client and Adaptee classes be given
      Client client = new Client();
      Adaptee adaptee = new Adaptee();

      // 2. Create an instance of the Adapter class, pass an instance of the Adaptee class
      //   to the constructor of the Adapter class
      ITarget target = new Adapter(adaptee);

      // 3. Pass target to the client
      client.ClientMethod(target);
    }
  }
}

  ⇑


Related topics

  ⇑