This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.
- Understand what design patterns are
- Learn about the myriad of design patterns that you can use in your projects
This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.
Basic knowledge of Java programming.
Hello dear friends. In this video, we will examine adapter design pattern, let's begin. While designing an object-oriented application, we might feel the need for an adapter when say 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 function through a different interface. The adapter design pattern is one of the structural design patterns and it's 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 a 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.
The second one is it allows the reusability of existing functionality. Theoretically, I believe that is enough for 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 result 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 getSpeed. 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 miles per hour to kilometers. Implements from MovableAdapter.
Override method. Now, add new method and name it, convertMPHtoKMH. We need a double parameter as miles per hour value. Now, we'll only use the methods to find in our adapter and we'll get the converted speeds. First, define a movable instance from Bugatti, connect with adapter. Now, send to screen. Done, let's try and run our app. You may remember our speed input was 300 miles per hour. Look, 300 miles per hour equals 482 kilometers per hour. So, our program runs flawlessly. Done, 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. Look forward to seeing you in the next video.
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.