Adapter Pattern



  • Adapter Pattern

    An adapter helps two incompatible interfaces, whose inner functionality should suit the need, to work together. The Adapter design pattern achieves this by converting the interface of one class into an interface expected by the clients.

    Intent

    • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
    • Wrap an existing class with a new interface.
    • Match an old component to a new system

    Implementation

    Class Diagram of Adapter Pattern
    The following example is adapted from this post.

    // Player.java
    interface Player {
       public void play(String type, String fileName);
    }
    
    // AudioPlayer.java
    class AudioPlayer {
       @Override
       public void playAudio(String fileName) {
          System.out.println("Playing. Name: "+ fileName);
       }
    }
    
    // VideoPlayer.java
    class VideoPlayer {
       @Override
       public void playVideo(String fileName) {
          System.out.println("Playing. Name: "+ fileName);
       }
    }
    
    // MyPlayer.java
    class MyPlayer implements Player {
    
       AudioPlayer audioPlayer = new AudioPlayer();
       VideoPlayer videoPlayer = new VideoPlayer();
    
       @Override
       public void play(String audioType, String fileName) {
          if(audioType.equalsIgnoreCase("avi")){
             videoPlayer.playVideo(fileName);
          }else if(audioType.equalsIgnoreCase("mp3")){
             audioPlayer.playAudio(fileName);
          }
       }
    }
    
    // Main.java
    public class Main {
       public static void main(String[] args) {
          Player myPlayer = new MyPlayer();
    
          myPlayer.play("mp3", "h.mp3");
          myPlayer.play("avi", "me.avi");
       }
    }
    

    Reference

    Wikipedia


登录后回复
 

与 BitTiger Community 的连接断开,我们正在尝试重连,请耐心等待