Adapter Design Pattern in Java

Tram Ho


In this article, I will introduce adapter design pattern design in Java. This is a type of structural design patterns, which allows two unrelated interfaces, with no common point to work together.
This model is easy to understand because the real world is full of adapters for us to visualize. For example, consider a USB to Ethernet converter. We need this when we have the Ethernet interface on one end and the other end as USB. Because they are not compatible with each other. We use an adapter to convert one to another. This example is quite similar to object-oriented adapters. In design, adapters are used when we have a class (Client) that expects some kind of object and we have an object (Adaptee) that provides similar features but exposes an interface. other.

Use adapter:

  • The client makes requests to the adapter by calling a method on it by the target interface.
  • The adapter translates the request on the adaptee using the interface of the adapter.
  • The client receives the result of the call and is unaware of the adapter’s presence.
    The adapter pattern converts the interface of a class into another interface that the client expects, which allows classes to work together that are not compatible with each other.
    Check out the diagram to get an overview of the structure of this design: The client sees the target interface, not the adapter. The adapter implements the target interface. The adapter delegates all the requirements to Adaptee.

Example 1:

Suppose you have a Bird class with fly() and makeSound() . And also a ToyDuck class with squeak() . Let’s assume that you lack ToyDuck objects and you want to use Bird objects in their place. Bird has some similar functions but implements a different interface, so we can use them directly. Therefore, we will use the adapter model. Here, the client will be ToyDuck and the adapter will be Bird .
Here is the source code of the example:

And here is the output of the example:

Explain: Here we have created a BirdAdapter class implementing inteface ToyDuck to convert Bird ‘s makeSound() method to makeSound() ‘s squeak() ToyDuck . This way, we can still call the squeak() of Bird objects through BirdAdapter
Before : ToyDuck toyDuck = new PlasticToyDuck();
After : ToyDuck toyDuck = new BirdAdapter(sparrow);
You can see that by changing just one line, toyDuck can now perform Chirp Chirp !!

Example 2

The example 1 above is a simple example so that we can easily imagine how the adapter holds an adaptee for conversion and communication. Now let’s move on to a slightly more complex example:
We have the MediaPlayer interface and the specific AudioPlayer class implement MediaPlayer . AudioPlayer can play mp3 audio files by default.
We have a different interface AdvancedMediaPlayer and specific classes implement interface AdvancedMediaPlayer . These classes can play vlc and mp4 files.
We want to create AudioPlayer to play other formats. To achieve this, we created a MediaAdapter class, which implements the MediaPlayer interface and uses the AdvancedMediaPlayer objects to play the required format.
AudioPlayer uses adapters of MediaAdapter class to convert to the desired audio type without knowing the actual class can play the desired format. AdapterPotypeDemo , our demo class will use AudioPlayer class to play different formats.
Here is the example class diagram:
The following is the source code for this example:

Output of example:


Share the news now

Source : Viblo