Adapter Pattern – Design Pattern

Adapter Patter

Here’s how the client uses the Adapter

  1. The client makes a request to the adapter by calling a method on it using the target interface.
  2. The adapter translates that request into one or more calls on the adaptee using the adaptee interface
  3. The client recieves the results of the call and never knows there is an adapter doing the transaltion.

    1. The Adaptar Pattern converts the interface of a class into another interface the client expects.
      Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.


      Now, we know this pattern allows us to use a client with an incompatible interface by creating
      an Adapter that does the conversion. This act to decouple the client from the implemented interface,
      and if we expect the interface to change over time, the adapter encapsulates that change so that the
      client doesn’t have to be modified each time it needs to operate against a different interface.

      Here is duck interface and its implementation

      package com.designpatter.adapter;
      
      /**
       *
       * @author taher
       */
      public interface Duck {
      
          public void quack();
      
          public void fly();
      }
      
      
      package com.designpatter.adapter;
      
      /**
       *
       * @author taher
       */
      public class MallardDuck implements Duck{
      
          public void quack() {
              System.out.println("Quack");
          }
      
          public void fly() {
              System.out.println("I'm Flying");
          }
      
      }
      
      

      Here is Turkey interface and its implementation

      package com.designpatter.adapter;
      
      /**
       *
       * @author taher
       */
      public interface Turkey {
      
          public void gobble();
      
          public void fly();
      }
      
      
      package com.designpatter.adapter;
      
      /**
       *
       * @author taher
       */
      public class WildTurkey implements Turkey{
      
          public void gobble() {
              System.out.println("Gobble gobble");
          }
      
          public void fly() {
              System.out.println("I'm flying a short distance");
          }
      
      }
      
      

      Here we make such a class(Adapter) which accepts Turkey as Wrapper and converts into duck adaptee

      package com.designpatter.adapter;
      
      /**
       *
       * @author taher
       */
      public class TurkeyAdaptar implements Duck {
      
          Turkey turkey;
      
          public TurkeyAdaptar(Turkey turkey) {
              this.turkey = turkey;
          }
      
          public void quack() {
              turkey.gobble();
          }
      
          public void fly() {
              for (int i = 0; i < 5; i++) {
                  turkey.fly(); //5 times as duck flies more than turkey 😉
              }
          }
      }
      
      

      Here’s actual implementation check output…

      package com.designpatter.adapter;
      
      /**
       *
       * @author taher
       */
      public class DuckTestDrive {
          public static void main(String[] args) {
              MallardDuck duck = new MallardDuck();
      
              WildTurkey turkey = new WildTurkey();
      
              TurkeyAdaptar turkeyAdaptar = new TurkeyAdaptar(turkey);
      
              System.out.println("Turkey says...");
              turkey.gobble();
              turkey.fly();
      
              System.out.println("Duck says...");
              testDuck(duck);
      
              System.out.println("TurkeyAdapter says...");
              testDuck(turkeyAdaptar);
      
              
          }
          static void testDuck(Duck duck){
              duck.quack();
              duck.fly();
          }
      }
      
      
      Output :

      Turkey says…
      Gobble gobble
      I’m flying a short distance
      Duck says…
      Quack
      I’m Flying
      TurkeyAdapter says…
      Gobble gobble
      I’m flying a short distance
      I’m flying a short distance
      I’m flying a short distance
      I’m flying a short distance
      I’m flying a short distance

      Advertisements
This entry was posted in Core JAVA, Design Pattern, Just 4 Knowledge. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s