Tuesday, March 7, 2023

Java adapter design pattern

Adapter design pattern

This pattern allows objects with incompatible interfaces to work together by wrapping one interface around the other.


The Adapter pattern is a design pattern in object-oriented programming that allows two incompatible interfaces to work together. In Java, the Adapter pattern is implemented using an Adapter class that acts as a bridge between two incompatible interfaces.

The Adapter class implements the interface that is expected by the client code, and it contains an instance of the class that implements the incompatible interface. The Adapter class then translates the methods of the client interface into the corresponding methods of the incompatible interface.

Here's an example implementation of the Adapter pattern in Java:


// Interface expected by the client interface MediaPlayer { public void play(String audioType, String fileName); } // Incompatible interface interface AdvancedMediaPlayer { public void playVlc(String fileName); public void playMp4(String fileName); } // Class that implements the incompatible interface class VlcPlayer implements AdvancedMediaPlayer { public void playVlc(String fileName) { System.out.println("Playing VLC file. Name: "+ fileName); } public void playMp4(String fileName) { // do nothing } } // Adapter class class MediaAdapter implements MediaPlayer { AdvancedMediaPlayer advancedMusicPlayer; public MediaAdapter(String audioType){ if(audioType.equalsIgnoreCase("vlc") ){ advancedMusicPlayer = new VlcPlayer(); } } public void play(String audioType, String fileName) { if(audioType.equalsIgnoreCase("vlc")){ advancedMusicPlayer.playVlc(fileName); } } } // Client code class AudioPlayer implements MediaPlayer { MediaAdapter mediaAdapter; public void play(String audioType, String fileName) { //play mp3 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")){ mediaAdapter = new MediaAdapter(audioType); mediaAdapter.play(audioType, fileName); } } } // Usage public class Main { public static void main(String[] args) { AudioPlayer audioPlayer = new AudioPlayer(); audioPlayer.play("mp3", "beyond_the_horizon.mp3"); audioPlayer.play("vlc", "far_far_away.vlc"); } }

In this example, the MediaPlayer interface is the interface expected by the client code. The AdvancedMediaPlayer interface is the incompatible interface, and the VlcPlayer class implements this interface.

The MediaAdapter class is the adapter that translates the methods of the MediaPlayer interface into the corresponding methods of the AdvancedMediaPlayer interface. The AudioPlayer class is the client code that uses the MediaPlayer interface.

When the client code calls the play() method on the AudioPlayer object, the adapter is used to play the file in the desired format. If the file is in the MP3 format, it is played directly by the AudioPlayer object. If the file is in the VLC format, the adapter is used to play the file by calling the play() method on the MediaAdapter object, which in turn calls the playVlc() method on the VlcPlayer object.

Labels:

REST and gRPC

REST vs. gRPC

REST (Representational State Transfer) and gRPC are two different approaches to creating APIs for building distributed systems. Here are some key differences between REST and gRPC:

  1. Protocol: REST uses HTTP as its underlying protocol, while gRPC uses the Protocol Buffers protocol. HTTP is a text-based protocol that is easy to use and widely supported, while Protocol Buffers is a binary protocol that is designed for efficient serialization and deserialization of structured data.

  2. Architecture: REST is an architectural style that emphasizes the use of resources and the HTTP verbs to perform operations on those resources. It is a stateless architecture that can be easily scaled and distributed. On the other hand, gRPC is a modern, high-performance, and open-source RPC (Remote Procedure Call) framework that enables communication between services using a range of programming languages.

  3. Payloads: REST typically uses JSON or XML as its payload format, while gRPC uses Protocol Buffers for payload serialization. Protocol Buffers are more efficient than JSON or XML, making gRPC more performant for high-volume, low-latency use cases.

  4. APIs: REST APIs are typically designed to be human-readable and easy to use, while gRPC APIs are designed to be machine-readable and optimized for performance.

In summary, REST and gRPC have different design goals and are suitable for different use cases. REST is more suited for building APIs that are easy to use and consume over HTTP, while gRPC is more suited for building high-performance, low-latency APIs that require efficient serialization and deserialization of structured data.

Labels: