Adapter Design Pattern Interview Questions

1 What is the Adapter Design Pattern?

The adapter design pattern comes under the structural design pattern’s category that allows incompatible objects to collaborate. This one acts as a wrapper between two varying objects. The adapter gets a hold on the call for one object and transforms it to be recognized by the second object.

Let’s consider the example of a USB to Ethernet adapter to understand it better. When you have an ethernet interface at one end and the USB interface on the other, you use USB to Ethernet. Since the ethernet and USB are not compatible with each other, you have to use an adapter.

This adapter class comes with a client class and an adaptee class. While the former expects an object type, the latter provides the same feature by exposing a distinct interface. Now, to establish communication between the two, there is an adapter class.

The client uses the target interface to request the adapter. And then, the adapter class translates the request through the adaptee interface. The client, then, receives the results without having any idea of the role of an adapter.

To get deeper into this concept, let’s consider another example. Suppose you have a MediaPlayer Interface that is implemented by the AudioPlayer class. The AudioPlayer can play the format of mp3 by default. Let’s consider another interface, which is AdvancedPlayer, implemented by the MP4Player class that plays the mp4 formats. And then, there is a WAVPlayer that plays wav formats.

If you want the AudioPlayer class to play different formats, you will have to use the MediaAdapter class that implements the MediaPlayer interface and makes use of the AdvancedPlayer objects to play the needed format.

The code implementation of this situation will be:

The output of the code will be:

1 What do you mean by inversion of control?

Inversion of Control (IoC) is one such pattern that is used to decouple the dependencies between the system’s components and layers. The Dependency-Injection (DI) pattern is one such example of this IoC pattern that assists in eradicating the dependencies in the entire code.

Let’s dive deeper with an example. Suppose we have a class Y that uses class Z as:

Here, we get a dependency between the Y and Z classes. If the IoC pattern was implemented, the new operator wouldn’t have been used to assign value to the dependent variable. And then, it would have been:

The control of managing the dependency of instantiating the object of class Z to the IoC class IocZ has been inverted.

Q4: What is Observer pattern?

Observer pattern (also known as Publish-Subscribe Pattern) is used when there is one-to-many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically. Observer pattern falls under behavioral pattern category.

An object with a one-to-many relationship with other objects who are interested in its state is called the subject or publisher. The observers are notified whenever the state of the subject changes and can act accordingly. The subject can have any number of dependent observers which it notifies, and any number of observers can subscribe to the subject to receive such notifications.

Observer pattern uses two actor classes:

  • The Observer (os Subscriber) abstract class provides an update() method which will be called by the subject to notify it of the subject’s state change.
  • The Subject (or Publisher) class is also an abstract class and defines four primary methods: attach(), detach(), setState(), and notify()
  • Adapter Design Pattern in detail | Interview Question

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *