Adapter Design Pattern

The adapter is a structural design pattern that enables the collaboration of objects with incompatible interfaces.

You can create an adapter. This is a special object that converts an object’s interface so that another object can understand it.

An adapter wraps one of the objects to hide the complexity of the conversion that occurs behind the scenes. The wrapped object is not even aware of the adapter. For example, you can wrap an object that operates in meters and kilometers with an adapter that converts all data to imperial units such as feet and miles.

Adapters can not only convert data into various formats, they can also help objects with different interfaces to collaborate. Is that how it works:

The adapter gets an interface, compatible with one of the existing objects.
With this interface, the existing object can safely call the adapter’s methods.
Upon receiving a call, the adapter passes the request to the second object, but in the format and order expected by the second object.
Sometimes it is even possible to create a bi-directional adapter that can convert calls in both directions.

Lets look at the example I created as below.

PHP Sample Code

<?php
/**
 * The original interface that is working as normal
 */
interface Socket
{
    public function input();
}
/**
 * This is the simple class that follows the existing target interface `Socket`
 */
class twoPinSocket implements Socket
{
    public function input()
    {
        echo "two pin input";
    }
}
/**
 * This is the conversion class (might be 3rd party service code as well) that will be used later in adapter class.
 */
class conversion
{
    public function changetoThreePin()
    {
        echo "changed to three pin => three pin input";
    }
}
/**
 * This is the adapter class implementing the original target interface linking with conversion class.
 * this will product the format that is second object want.
 */
class threePinSocket implements Socket
{
    private $conversion;
    public function __construct(conversion $conversion)
    {
        $this->conversion = $conversion;
    }
    public function input()
    {
        $this->conversion->changetoThreePin();
    }
}
/**
 * The existing twoPinSocket class that follows the target interface.
 */
$socket = new twoPinSocket();
echo $socket->input();
/**
 * threePinSocket conversion that follows target interface using conversion adapter
 */
$conversion = new conversion();
$socket = new threePinSocket($conversion);
echo $socket->input();
?>

C# Sample Code

using System;
namespace HelloWorld
{
    /**
     * The original interface that is working as normal
     */
    public interface Socket
    {
        string input();
    }
    /**
     * This is the simple class that follows the existing target interface `Socket`
     */
    class TwoPinSocket : Socket
    {
        public string input()
        {
            return "two pin input";
        }
    }
    /**
     * This is the conversion class (might be 3rd party service code as well) that will be used later in adapter class.
     */
    class Conversion
    {
        public string change()
        {
            return "changed 2pin to 3pin";
        }
    }
    /**
     * This is the adapter class implementing the original target interface linking with conversion class.
     * this will product the format that is second object want.
     */
    class ThreePinSocket : Socket
    {
        private readonly Conversion _conversion;
        public ThreePinSocket(Conversion conversion)
        {
            this._conversion = conversion;
        }
        public string input()
        {
            return this._conversion.change();
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            /**
             * The existing twoPinSocket class that follows the target interface.
             */
            Socket socket = new TwoPinSocket();
            Console.WriteLine(socket.input());
            /**
             * threePinSocket conversion that follows target interface using conversion adapter
             */
            Conversion conversion = new Conversion();
            Socket socket = new ThreePinSocket(conversion);
            Console.WriteLine(socket.input());
        }
    }
}

By Yuuma



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム