Monday, March 27, 2023

Examples Of Adapter Design Pattern in JDK

 Here are some examples of Java API that make use of Adapter design pattern:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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 the Arrays.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 list object 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 InputStreamReader class is an adapter that adapts the InputStream interface (which reads bytes) to the Reader interface (which reads characters).

    The InputStream interface 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. The Reader interface provides methods to read characters from an input source. Therefore, we need an adapter to convert the bytes read by the InputStream into characters that can be read by the Reader.

    The InputStreamReader class does this by implementing the Reader interface and delegating the read() and read(char[], int, int) methods to the read() method of the InputStream interface. The read() method of the InputStream interface returns a byte, so the InputStreamReader class converts the byte to a character using a specified character encoding (such as UTF-8) before returning it to the caller.

    Here's an example code snippet that shows how InputStreamReader adapts an InputStream to a Reader:

    // 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 InputStream object inputStream reads bytes from a file. The InputStreamReader object reader adapts the inputStream to a Reader that can read characters. The while loop reads characters from the reader one by one and does something with each character. The InputStreamReader adapter pattern allows us to read characters from an input stream that would otherwise only be able to read bytes.

     

     

 

augustine

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 Java Tutorial.

Designed by Mathew | mathai