Is this an acceptable implementation of the Adapter pattern? I've used the UK Plug -> US Socket and US Plug -> UK Socket analogy. I've also made it possible for UK plug to use a UK socket and a US plug to use a US socket. The output of the SSCCE below is

UKPlug -&gt; UKSocket
USPlug -&gt; USSocket
UKPlug -&gt; UKUSAdapter -&gt; USSocket
USPlug -&gt; USUKAdapter -&gt; UKSocket


And the code

package com.structural.adapter.adapter2;

interface IAdapter {
  public void plugin();
}

class UKUSAdapter implements IAdapter {
  UKPlug plug;
  USSocket socket;
  public UKUSAdapter(UKPlug plug, USSocket socket) {
    this.plug = plug;
    this.socket = socket;
  }

  @Override
  public void plugin() {
    System.out.print("UKUSAdapter -&gt; ");
    socket.accept_2pin_plug(plug.pin1, plug.pin2);
  }
}

class USUKAdapter implements IAdapter {
  USPlug plug;
  UKSocket socket;
  public USUKAdapter(USPlug plug, UKSocket socket) {
    this.plug = plug; 
    this.socket = socket;
  }

  @Override
  public void plugin() {
    System.out.print("USUKAdapter -&gt; ");
    socket.accept_3pin_plug(plug.pin1, plug.pin2, 1);
  }

}

class USSocket {
  USPlug plug;
  public USSocket(USPlug plug) {
    this.plug = plug;
  }

  public void accept_2pin_plug(int pin1, int pin2) {
    System.out.print("USSocket");
  }
}

class UKSocket {
  UKPlug plug;
  public UKSocket(UKPlug plug) {
    this.plug = plug;
  }
  public void accept_3pin_plug(int pin1, int pin2, int pin3) {
    System.out.print("UKSocket");
  }
}

class UKPlug {   
  int pin1;
  int pin2;
  int pin3;

  IAdapter adapter;
  UKSocket socket;

  public UKPlug(int pin1, int pin2, int pin3) {
    this.pin1 = pin1;
    this.pin2 = pin2;
    this.pin3 = pin3;
  }

  public void plug_into_3pin() {
    System.out.print("UKPlug -&gt; ");
    if(this.socket == null) {
      adapter.plugin(); 
    } else {
     socket.accept_3pin_plug(pin1, pin2, pin3); 
    }    
  }

  public void setAdapter(IAdapter adapter) {
    this.adapter = adapter;
    this.socket = null;    
  }

  public void setSocket(UKSocket socket) {
    this.socket = socket;
    this.adapter = null;
  }
}

class USPlug {   
  int pin1;
  int pin2;

  IAdapter adapter;
  USSocket socket;

  public USPlug(int pin1, int pin2) {
    this.pin1 = pin1;
    this.pin2 = pin2;
  }

  public void plug_into_2pin() {
    System.out.print("USPlug -&gt; ");
    if(this.socket == null) {
      adapter.plugin(); 
    } else {
      socket.accept_2pin_plug(pin1, pin2);
    }    
  }

  public void setAdapter(IAdapter adapter) {
    this.adapter = adapter;
    this.socket = null;
  }

  public void setSocket(USSocket socket) {
    this.socket = socket;
    this.adapter = null;
  }
}

public class TestPlugAdapters {
  public static void main(String[] args) {
    UKPlug ukPlug = new UKPlug(1,1,1);
    UKSocket ukSocket = new UKSocket(ukPlug);

    USPlug usPlug = new USPlug(1,1);
    USSocket usSocket = new USSocket(usPlug);

    //Test UK Plug to UK Socket
    ukPlug.setSocket(ukSocket);
    ukPlug.plug_into_3pin();
    System.out.println();

    //Test US Plug to US Socket
    usPlug.setSocket(usSocket);
    usPlug.plug_into_2pin();
    System.out.println();

    //Test UK Plug to US Socket
    IAdapter ukusAdapter = new UKUSAdapter(ukPlug, usSocket);
    ukPlug.setAdapter(ukusAdapter);
    ukPlug.plug_into_3pin();
    System.out.println();

    //Test US Plug to UK Socket
    IAdapter usukAdapter = new USUKAdapter(usPlug, ukSocket);
    usPlug.setAdapter(usukAdapter);
    usPlug.plug_into_2pin();
    System.out.println();
  }
}
This is not an implementation of the Adapter pattern.

What is the Adapter pattern trying to solve?

Lets' say I have :

public interface UKPlug {

    int getPin1();

    int getPin2();

    int getPin3();
}

public class UKSocketImpl {

    public void plugin(UKPlug plug) {

        if (plug.getPin1() == -plug.getPin2() &amp;&amp; plug.getPin3() == 0) {
            System.out.println(plug.toString() + " -&gt; UKSocket");
        }

    }
}


A UK socket, that accepts UKPlugs. The socket is the client in this scenario, which makes use of an interface (UKPlug)

Now I want to plug in my device, but it has a USPlug :

import java.math.BigInteger;

public class USPlugImpl {

    public BigInteger getPin1() {
        return BigInteger.TEN;
    }

    public BigInteger getPin2() {
        return BigInteger.TEN.negate();
    }

    @Override
    public String toString() {
        return "USPlug";
    }
}


This is where we can write an Adapter class to adapt our Adaptee (USPlugImpl) :

package adapter.improved;

public class USUKAdapter implements UKPlug {

    private final USPlugImpl plug;

    public USUKAdapter(USPlugImpl plug) {
        this.plug = plug;
    }

    @Override
    public int getPin1() {
        return plug.getPin1().intValue();
    }

    @Override
    public int getPin2() {
        return plug.getPin2().intValue();
    }

    @Override
    public int getPin3() {
        return 0;
    }

    @Override
    public String toString() {
        return plug.toString() + " adapted to UKPlug";
    }
}


This is what the Adapter class does :


it implements the needed interface
it does so by delegating to methods of the Adaptee instance (the USPlug), or by supplying some default behaviour.
Both the USPlug as the UKSocket are unaware of the use of an Adapter.


example use :

private void plugIn(UKSocketImpl ukSocket, USPlugImpl usPlug) {
    ukSocket.plugin(new USUKAdapter(usPlug));
}


How do your Adapter classes differ?


They implement an interface, but not one that is used by the Client
The UKPlug and USPlug classes have a reference to an adapter. What if these were classes from a 3rd party library, and you couldn't add members to them? That's what the Adapter pattern solves.


Final style note : don't use '_' in method names.