Adapter pattern: two interfaces : Adapter Pattern « Design Pattern « Java Tutorial






public class TestAdapter {
  public static void main(String args[]) {
    SimpleName aceObject = new SimpleName();

    aceObject.setName("A");

    FullNameSimpleNameAdapter adapter = new FullNameSimpleNameAdapter(aceObject);

    System.out.println("Customer's first name: " + adapter.getFirstName());
    System.out.println("Customer's last name: " + adapter.getLastName());
  }
}

interface SimpleNameInterface {
  public void setName(String n);

  public String getName();
}

class SimpleName implements SimpleNameInterface {
  String name;

  public void setName(String n) {
    name = n;
  }

  public String getName() {
    return name;
  }
}

interface FullNameInterface {
  public void setFirstName(String f);

  public void setLastName(String l);

  public String getFirstName();

  public String getLastName();
}

class FullNameSimpleNameAdapter implements FullNameInterface {
  SimpleName aceObject;
  String firstName;
  String lastName;

  public FullNameSimpleNameAdapter(SimpleName a) {
    aceObject = a;
    firstName = aceObject.getName().split(" ")[0];
    lastName = aceObject.getName().split(" ")[1];
  }

  public void setFirstName(String f) {
    firstName = f;
  }

  public void setLastName(String l) {
    lastName = l;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }
}








34.4.Adapter Pattern
34.4.1.Adapter Pattern Demo
34.4.2.Variations on the Adapter pattern.
34.4.3.Adapter pattern: two interfaces