Variations on the Adapter pattern. : Adapter Pattern « Design Pattern « Java Tutorial






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








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