September 30, 2011

Adapter Design Pattern code example in Java

Adaptee Concrete

public class Plug {
   private String specification = "5 AMP";
   public String getInput() {
      return specification;
   }
}

Target interface

public interface Socket {
   public String getOutput();
}

Adapter Concrete

public class PlugAdapter implements Socket {

   Plug plug;

   public String getOutput() {
      plug = new Plug();
      String output = plug.getInput();
      return output;
   }
}

Client

public class Client {
   Socket socket;

   public void m1() {
      socket = new PlugAdapter();
      socket.getOutput();
   }
}

No comments:

Post a Comment