Design Patterns Related Interview Questions - Adapters

Detailed explanation of adapter mode:

  • The adapter pattern definition
    converts one interface into another interface expected by the client. The adapter pattern enables those classes whose interfaces are not compatible to work together, and its alias is a wrapper (Wrapper).
  • Class adapter
    ①, class adapter definition:
    The class adapter mode converts the API of the adapted class into the API of the target class.
    ②、Analysis of UML structure diagram:

    ③、Detailed explanation of code:
    First define the Target interface:
    public interface Target {
    
        void sampleOperation1 ();
    
        void sampleOperation2();
    }

    Re-define the class Adaptee:

    public class Adaptee {
        public void sampleOperation1() {
            System.out.println("sampleOperation1");
        }
    }

    Then define the Adapter class:

    public class Adapter extends Adaptee implements Target {
        @Override
        public void sampleOperation2() {
            System.out.println("sampleOperation2");
        }
    }
    Apply it:

    ④. Summary:
    a. The way that the class adapter uses object inheritance is a static way of definition.
    b. For class adapters, the adapter can redefine part of the behavior of Adaptee.
    c. For the class adapter, only one object is introduced, and no additional reference is needed to obtain the Adaptee indirectly.
    d. For class adapters, since adapters directly inherit Adaptee, using adapters cannot work with subclasses of Adaptee.
  • Object adapter
    ①. Object adapter definition:
    Like the class adapter mode, the object adapter mode converts the API of the adapted class into the API of the target class. Unlike the class adapter mode, the object adapter mode does not use inheritance. The relationship is connected to the Adaptee class, but instead is connected to the Adaptee class using a delegation relationship.
    ②, UML structure diagram analysis:

    ③, code detailed explanation:
    its Target interface is still the same as the previous class adapter:
    public interface Target {
    
        void sampleOperation1 ();
    
        void sampleOperation2();
    }

    Adaptee is also the same as the previous class adapter:

    public class Adaptee {
        public void sampleOperation1() {
            System.out.println("sampleOperation1");
        }
    }
    Then the difference comes, look at the Adapter class at this time:
    public class Adapter implements Target {
    
        private Adaptee adaptee;
    
        public Adapter(Adaptee adaptee) {
            this.adaptee = adaptee;
        }
    
        @Override
        public void sampleOperation1() {
            adaptee.sampleOperation1();
        }
    
        @Override
        public void sampleOperation2() {
            System.out.println("sampleOperation2");
        }
    }

    Hold a reference to Adaptee instead of inheriting from it, and then apply:

    ④. Summary:
    a. The way the object adapter uses object composition is the way of dynamic composition.
    b. For object adapters, an adapter can adapt a variety of different sources to the same target.
    c. For object adapters, it is difficult to redefine the behavior of Adaptee.
    d. For object adapters, additional references are required to obtain Adaptee indirectly.

The actual application of adapter mode in android:

ListView uses the adapter mode. I don't want to analyze it too much. It is divided into the following two points:

  • The UI
    is first on the picture:

    1. The layout of the ListView is composed of items one by one, and each item is a View. The View is added to the ListView through the Adapter adapter.
    2. An Adapter is the bridge between the AdapterView view and the data. The Adapter provides access to the data and is also responsible for generating a corresponding View for each item of data.
    3. After each item of data generates the corresponding View, then add the View to the ListView.
    4. It belongs to the MVC architecture, of which Adapter belongs to C.
  • The source code
    is similar to ListView's recycleBin, and its adapter is defined in the parent class of ListView, as follows:

    At this point, the following code is seen in the onAttachedToWindow() method:

    In this method, another pattern was also found: the observer, as follows:

    This pattern is not the focus of this discussion, and will be specifically studied in the future.
    At this point, the focus is shifted here:

    So go back to the layoutChildren() method of ListView to check, but its code is too large, just pick the key points to understand:

    Then look at the fillUp() method:

    Now locate the makeAndAddView() method, which defines it:

    and this method is actually defined in its parent class AbsListView, as follows:

    And the amount of code of this method is also very large, continue to focus on the analysis, as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325349468&siteId=291194637