A Complete Guide To Design Patterns: The Adapter Design Pattern
The adapter design pattern is a structural design pattern that's very useful when maintaining a legacy code or adding a new features.
Filter by Category
Filter by Author
The adapter design pattern is a structural design pattern that's very useful when maintaining a legacy code or adding a new features.
Posted by Baraa Abuzaid
The Façade design pattern is a structural design pattern. It reduces coupling and improves code readability.
Posted by Baraa Abuzaid
Will be going through the Creational Design Pattern illustrating how and where to use it.
Posted by Baraa Abuzaid
This tutorial will explain the fundamentals of ViewModel and LiveData and how to use them by creating simple demo App that shows recent movies.
Posted by Baraa Abuzaid
This is a brief look into MVP clean architecture discussing why it is the best architecture for android development
Posted by Baraa Abuzaid
This tutorial will illustrate how to implement infinite scrolling in Android, where the data should be load automatically as the user scrolls down
Posted by Baraa Abuzaid
Example that goes through making a UML class diagram from simple problem statement
Posted by Baraa Abuzaid
This is a part of a blog series that will go through all you need to know about UML class Diagram to get up and running quickly
Posted by Baraa Abuzaid
The adapter design pattern is a structural design pattern that's very useful when maintaining a legacy code or adding a new features.
The adapter design pattern is a structural design pattern. As the name drove from the adapter in the physical world, the adapter design pattern work in a similar way. In many cases, our app needs to interact with other systems or libraries that we can’t change it or even we might need to maintain a legacy system that might break if it undergoes any changes.
In these circumstances the use of the adapter design pattern shine.
Furthermore, In the adapter design pattern, we have an adapter that is responsible to mediate the communication between an outside system or service (the Adaptee) and a client. Meanwhile, internally the adapter class wraps the adaptee and expose its functionality by adhering to an interface that the client is familiar with. So by doing that a client class will be able to use a new service or library (the adaptee) without any modifications.
To make things clear let’s take a look at the adapter pattern in UML class diagram. And if you ain’t familar with UML please check out my UML Class diagram tutorial
So consider a situation in which you have a web client class in your app trying to interact with the remote server. The endpoint only accepts a JSON. Now you should do some sort of conversion from a plain old java object (POJO) to JSON. At some point in time, the endpoint changed to only accept XML. if you are about to anticipate such a scenario in your app the perfect design pattern to use would be an adapter design pattern. Furthermore, we could have JSON Adapter, XML Adapter and we could use these adapters interchangeably without any need to modify our web client class.
Now we can illustrate the example in UML Class diagram as below.
IWebRequest Interface
public interface IWebRequest {
int request(Object Data);
}
JsonRequestAdapter Class
public class JsonRequestAdapter implements IWebRequest {
WebServices webServices;
public JsonRequestAdapter(WebServices webServices) {
this.webServices = webServices;
}
@Override
public int request(Object data) {
Json body = pojoToJson(data);
Json response = webServices.postRequest(body);
if(response != null){
return 200; // success status code
}
return 500; // Error status code
}
public void connect(String url){
// connecting to the end point ...
}
public Json pojoToJson(Object data){
// code for converting to json ...
return null;
}
}
WebClient Class
public class WebClient {
IWebRequest webRequest;
public WebClient(IWebRequest webRequest) {
this.webRequest = webRequest;
}
private Object createAttachment(String content){
// create plan old java object ...
return null;
}
public void upload(String content){
Object data = createAttachment(content);
int resultCode = webRequest.request(data);
if (resultCode == 200){
System.out.println("Status: Ok!");
}else {
System.out.println("Status: Error!");
}
}
}
Main Class
public class Main {
public static final void main(String[] args){
WebServices webServices = new WebServices();
JsonRequestAdapter jsonRequestAdapter = new JsonRequestAdapter(webServices);
jsonRequestAdapter.connect("https://baraabytes.com");
WebClient webClient = new WebClient(jsonRequestAdapter);
webClient.upload("User data");
}
}
Photo By Steve Johnson
A composite design pattern is built upon two principles in object-oriented programming. First, decomposition by breaking a whole into parts. Then generalization which generalizing...
A Proxy design pattern is a structural design pattern. the idea behind is to make a proxy object that is capable of performing tasks similar to the original object. The need for...
Thanks alot for very nice explaination with real life example. I have found one more blog and would like to share for other readers, who want to read more. Adapter design pattern in java