Here are some examples of Java API that make use of Adapter design pattern:
java.util.Arrays#asList(Object[]): This method returns a List that is backed by an array. Here, the Array is the adaptee, and the List is the target interface.
java.io.InputStreamReader(InputStream): This constructor reads bytes from the InputStream and converts them into characters using a specified character encoding. The InputStream is the adaptee, and the Reader interface is the target.
javax.xml.bind.annotation.adapters.XmlAdapter: This is an abstract class that can be extended to create a custom adapter to marshal and unmarshal XML elements. Here, the XML data is the adaptee, and the Java objects are the target interface.
java.awt.event.MouseAdapter: This is a class that extends the MouseAdapter abstract class and provides default implementations for all of the methods in the MouseListener interface. This adapter can be used to avoid having to implement all of the methods in the interface when only a few of them are needed.
java.util.zip.ZipOutputStream: This class provides a way to write compressed data to a file in the ZIP file format. The ZipOutputStream class adapts the FileOutputStream to write data in the ZIP format.
java.sql.ResultSet: This interface provides methods to access data from a database. The ResultSet interface can be adapted using the ResultSetMetaData interface to get information about the columns in the ResultSet.
Detailed Explanation of
java.util.Arrays#asList(Object[]).String[] array = {"apple", "banana", "orange"};
List<String> list = Arrays.asList(array);In this code, we have an array of strings (
array) and we want to convert it into a list. We can use theArrays.asList()method to do this. This method takes an array as its argument and returns a list that is backed by the array.Here, the array is the adaptee, and the List interface is the target. The
Arrays.asList()method adapts the array to the List interface by creating a List object that uses the array as its backing store. This allows us to use the List interface to manipulate the array as if it were a list.For example, we can use the
listobject to add or remove elements from the array:list.add("grape");
list.remove("apple");
In this code, we add the "grape" element to the list and remove the "apple" element from the list. These changes are reflected in the original array, since the list is backed by the array.
However, there are some limitations to using
Arrays.asList(). For example, the resulting list is fixed-size, so we cannot add or remove elements from the list if the underlying array cannot be resized. Additionally, changes made to the list (such as adding or removing elements) may not be reflected in the original array if the array is modified in some other way.Detailed Explanation of
java.io.InputStreamReader(InputStream):The
InputStreamReaderclass is an adapter that adapts theInputStreaminterface (which reads bytes) to theReaderinterface (which reads characters).The
InputStreaminterface is the adaptee in this pattern. It provides methods to read bytes from a stream of input data. However, in many cases, we want to read characters instead of bytes. TheReaderinterface provides methods to read characters from an input source. Therefore, we need an adapter to convert the bytes read by theInputStreaminto characters that can be read by theReader.The
InputStreamReaderclass does this by implementing theReaderinterface and delegating theread()andread(char[], int, int)methods to theread()method of theInputStreaminterface. Theread()method of theInputStreaminterface returns a byte, so theInputStreamReaderclass converts the byte to a character using a specified character encoding (such asUTF-8) before returning it to the caller.Here's an example code snippet that shows how
InputStreamReaderadapts anInputStreamto aReader:// create an input stream that reads bytes from a file
InputStream inputStream = new FileInputStream("input.txt");
// create an InputStreamReader that adapts the input stream to a Reader
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
// read characters from the Reader
int character;
while ((character = reader.read()) != -1) {
// do something with the character
}
In this example, the
InputStreamobjectinputStreamreads bytes from a file. TheInputStreamReaderobjectreaderadapts theinputStreamto aReaderthat can read characters. Thewhileloop reads characters from thereaderone by one and does something with each character. TheInputStreamReaderadapter pattern allows us to read characters from an input stream that would otherwise only be able to read bytes.