Adapter Design Pattern
Start course
Difficulty
Beginner
Duration
3h 59m
Students
131
Ratings
5/5
starstarstarstarstar
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello, dear friends. In this video, we will examine the Adapter Design Pattern. So, let's begin. While designing an object-oriented application, we might feel the need for an adapter when our client expects an object of a specific type and we have a third-party API offering the same functionality, but through an incompatible interface. The adapter design pattern is a structural design pattern that helps us to connect to the legacy or third-party code that exposes a similar functionality through a different interface. 

The adapter design pattern is one of the structural design patterns and its used so that two unrelated interfaces can work together. The object that joins this unrelated interface is called an adapter. This pattern is easy to understand as the real world is full of adapters. For example, consider a USB to Ethernet adapter. We need this when we have an Ethernet interface on one end and USB on the other. Since they are incompatible with each other, we use an adapter that converts one to another. This example is pretty analogous to object-oriented adapters. In design, adapters are used when we have a class, client, expecting some type of object, and we have an object, adaptee, offering the same features but exposing a different interface.

In other words, to provide the interface according to client requirements while using the services of a class with a different interface. If you look at the adapters UML diagram, you can see how the adapter works. The client makes a request to the adapter by calling a method on it using the target interface. The adapter translates that request on the adaptee using the adaptee interface. The client receives the results of the call and is unaware of the adapter's presence. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. 

For example, a real life example could be a case of a card reader which acts as an adapter between the memory card and a laptop. You plug in the memory card into the card reader and the card reader into the laptop so that the memory card can be read via laptop. Basically, there are two important benefits of the adapter pattern. First, it allows two or more previously incompatible objects to interact and the second one is, it allows the reusability of existing functionality. So, I think you get it about the adapter pattern. Let's see how we can use the adapter pattern in a project. Create a new project and name it Adapter pattern. As an example, let's say there's an app developed in the United States that returns the top speed of luxury cars in miles per hour.

Now, we create the same app for a client in the United Kingdom who requires the same results but in kilometers per hour. Now, we will start by creating a structure applying the adapter design pattern. We'll create an interface and name it "Movable." This interface will return the speed of some luxury cars in miles per hour. Next, we'll create a method and name it speed. Now, we will create our first car class and name it Bugatti. Override, getSpeed. Next, we'll create our adapter and name it MOvableAdapter. Now, we will add our adapter class to convert MPH to KMH. Implements from MovableAdapter.

Now, add new method and name it convertMPHtoKMH. We need a double parameter as MPH value. Override method. Now, we'll only use the methods defined in our adapter and we'll get the converted speeds. First, define a movable instance from Bugatti. Connect with adapter. Now, send to screen. Okay, done. Let's try. First, as you see our speed is 300 MPH. Look, 300 MPH = 482 KM/HR. Done. Okay, I hope this example has helped you understand the adapter pattern better. In the following video, we will look at the bridge pattern in Java. So, I look forward to seeing you there.

 

About the Author
Students
1996
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.