Java Tutorial/Design Pattern/Adapter Pattern

Материал из Java эксперт
Перейти к: навигация, поиск

Adapter Pattern Demo

class Target {
  public void request() {
  }
}
class Adaptee {
  public void specificRequest() {
    System.out.println("Adaptee: SpecificRequest");
  }
}
class Adapter extends Target {
  private Adaptee adaptee;
  public Adapter(Adaptee a) {
    adaptee = a;
  }
  public void request() {
    adaptee.specificRequest();
  }
}
public class SimpleAdapter {
  public static void main(String args[]) {
    Adaptee a = new Adaptee();
    Target t = new Adapter(a);
    t.request();
  }
}





Adapter pattern: two interfaces

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





Variations on the Adapter pattern.

class Programming {
  public void code() {
  }
  public void debug() {
  }
}
interface ModernProgramming {
  void ide();
}
class SurrogateAdapter implements ModernProgramming {
  Programming programming;
  public SurrogateAdapter(Programming wih) {
    programming = wih;
  }
  public void ide() {
    programming.code();
    programming.debug();
  }
}
class DailyJob {
  public void work(ModernProgramming wiw) {
    wiw.ide();
  }
}
class Coder extends DailyJob {
  public void op(Programming wih) {
    new SurrogateAdapter(wih).ide();
  }
}
class Coder2 extends Programming implements ModernProgramming {
  public void ide() {
    code();
    debug();
  }
}
class Coder3 extends Programming {
  private class InnerAdapter implements ModernProgramming {
    public void ide() {
      code();
      debug();
    }
  }
  public ModernProgramming whatIWant() {
    return new InnerAdapter();
  }
}
public class AdapterVariations {
  public static void main(String args[]) {
    DailyJob whatIUse = new DailyJob();
    Programming whatIHave = new Programming();
    ModernProgramming adapt = new SurrogateAdapter(whatIHave);
    Coder coder = new Coder();
    Coder2 coder2 = new Coder2();
    Coder3 coder3 = new Coder3();
    whatIUse.work(adapt);
    // Approach 2:
    coder.op(whatIHave);
    // Approach 3:
    whatIUse.work(coder2);
    // Approach 4:
    whatIUse.work(coder3.whatIWant());
  }
}