WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Design Patterns · Intermediate · question 36 of 100

How does the Proxy pattern differ from the Adapter pattern in terms of intent and implementation?

📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

Both the Proxy pattern and the Adapter pattern are part of the structural design patterns category, and while they might look similar at the first glance, they have different intents and implementations.

#### Proxy Pattern:
The Proxy pattern aims to provide a substitute or placeholder for another object to control access to it. In simple words, we can say that a proxy is a stand-in or placeholder for another object. It provides the same interface as the real object, so we can interchange them without changing the code that uses them. The proxy class acts as an intermediary between the client and the target object, hiding any implementation details of the target object from the client.

#### Implementation of Proxy Pattern:

The Proxy pattern follows the same interface as the target object, and also holds a reference to a target object. The Proxy object, before forwarding calls to target object, can add its functionality like caching, logging, or controlling the access to target object. This shielding of target object provides a level of security since the client can’t access the target object directly. The following is a simple implementation of the proxy pattern:

// Subject Interface
public interface Image {
    void display();
}

// Real Object implements the subject interface
public class RealImage implements Image {
    private String filename;

    public RealImage(String filename) {
        this.filename = filename;
        loadImageFromDisk();
    }

    private void loadImageFromDisk() {
        System.out.println("Loading " + filename);
    }

    public void display() {
        System.out.println("Displaying " + filename);
    }
}

// Proxy class implements the subject interface
public class ProxyImage implements Image {
    private Image realImage;
    private String filename;

    public ProxyImage(String filename) {
        this.filename = filename;
    }

    public void display() {
        if (realImage == null) {
            realImage = new RealImage(filename);
        }
        realImage.display();
    }
}

#### Adapter Pattern:

The Adapter pattern aims to convert one interface into another interface, so that two different incompatible classes can interact with each other. To put it simply, an adapter class is like a language translator, allowing two incompatible interfaces to communicate with each other.

#### Implementation of Adapter Pattern:

The Adapter pattern implements the Target interface and instantiates an adapter object that wraps an Adaptee object, which implements the Adaptee interface. The Target interface specifies the client’s requirements while the Adapter interface implements the Adaptee interface for satisfying the client request. An example implementation of the Adapter pattern is given below:

//Target Interface
interface AdvancedMediaPlayer {
   void playVlc(String fileName);
   void playMp4(String fileName);
}

//Adaptee classes
class VlcPlayer implements AdvancedMediaPlayer {
   public void playVlc(String fileName) {
      System.out.println("Playing vlc file. Name: "+ fileName);
   }

   public void playMp4(String fileName) {
      //do nothing
   }
}

class Mp4Player implements AdvancedMediaPlayer{

   public void playVlc(String fileName) {
      //do nothing
   }

   public void playMp4(String fileName) {
      System.out.println("Playing mp4 file. Name: "+ fileName);
   }
}

//Adapter class
class MediaAdapter implements MediaPlayer {

   AdvancedMediaPlayer advancedMusicPlayer;

    public MediaAdapter(String audioType){
      if(audioType.equalsIgnoreCase("vlc") ){
         advancedMusicPlayer = new VlcPlayer();

      }else if (audioType.equalsIgnoreCase("mp4")){
         advancedMusicPlayer = new Mp4Player();
      }
   }

   public void play(String audioType, String fileName) {

      if(audioType.equalsIgnoreCase("vlc")){
         advancedMusicPlayer.playVlc(fileName);
      }
      else if(audioType.equalsIgnoreCase("mp4")){
         advancedMusicPlayer.playMp4(fileName);
      }
   }
}

//Target class
class AudioPlayer implements MediaPlayer {
   MediaAdapter mediaAdapter;

   public void play(String audioType, String fileName) {

      //inbuilt support to play mp3 music files
      if(audioType.equalsIgnoreCase("mp3")){
         System.out.println("Playing mp3 file. Name: "+ fileName);
      }

      //mediaAdapter is providing support to play other file formats
      else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){
         mediaAdapter = new MediaAdapter(audioType);
         mediaAdapter.play(audioType, fileName);
      }

      else{
         System.out.println("Invalid media. " + audioType + " format not supported");
      }
   }
}

#### Key Differences:

The Proxy pattern acts as a placeholder or stand-in for an object, while the Adapter pattern acts as a wrapper for an object.

The Adapter pattern is used to convert one interface into another, while the Proxy pattern is used to implement control behavior of the target object through the proxy object.

Proxy pattern shields target object from direct access by the client, while the adapter pattern does not necessarily do that.

In Proxy pattern, the proxy object has the same interface as the target object, while in adapter pattern, the adapter object is wrapped around adaptee object with a different interface that satisfies the client’s requirement.

To sum up, the Proxy pattern is used as a placeholder or a wrapper for another object, while the Adapter pattern is used as a bridge between two incompatible interfaces.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview — then scores it.
📞 Practice Design Patterns — free 15 min
📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

All 100 Design Patterns questions · All topics