Design mode --- adapter mode

Article catalog


The adapter is mainly used for interface transformation or to combine the class objects incompatible with the interface to form an external unified interface. It is a structural pattern, and its essence is a middleware, which is applicable to classes and their objects.
This paper hopes that through a simple introduction and analysis, readers can have a simple and intuitive understanding and perception of adapter mode.

1. Purpose

Transform the interface of an existing class to meet the new requirements.

2. Motivation

Through transformation or combination, the existing functional modules can be reused indirectly to fulfill the requirements.

3. Advantages and disadvantages

advantage:

It improves the reuse of classes;
Combining several related objects to form an interface for providing unified services;
Good scalability and flexibility.
Disadvantages:

Excessive use of adaptation patterns can easily lead to confusion of code function and logic meaning.
Some languages may limit inheritance to only one adapter class, and the target class must be an abstract class.

4. Classification

Class Adapter
object adapter
Interface adapter
This paper mainly introduces the first two.

5. Main uses and scenarios

This pattern is not considered in the design and development stage, but mainly used when you want to modify an existing interface or combine several associated objects.

You want to use an existing class, but its interface does not meet the requirements;
Want to create a reusable class, which can work with other unrelated classes;
You want to use some existing subclasses, but you can't subclass each one to match their interfaces (only for object adapters). An object Adapter can adapt its parent interface.

6. Principle

The following is the UML class diagram of typical class adapter pattern and object adapter pattern introduced by GoF

Class Adapter

Principle: implement adaptation through class inheritance, inherit the interface of Target, and inherit the implementation of adapter

object adapter

Principle: adaptation through class object composition

Target:

Define the interface that the Client really needs to use.

Adaptee:

It defines an existing interface, which we need to adapt.

Adapter:

The interface between adapter and target is adapted to ensure that the call to the interface in target can be indirectly converted into the call to the interface in adapter.

7. Implementation

Next, the UML class diagram above is transformed into two specific examples, and then a specific example is used for each type
Let's use a few examples to actually experience the application of proxy mode.

Adapter type 7.1

Define Target interface class: Target

 public interface Target {
    void request();
}

Adapted class: Adaptee

public class Adaptee {
    public void adapteeRequest() {
        System.out.println("adapteeRequest method of Adaptee! ");
    }
}

The Adapter class inherits the interface request of Target and the implementation of adapterequest of Adaptee

public class Adapter extends Adaptee implements Target {
    @Override
    public void request() {
        // TODO Auto-generated method stub
        super.adapteeRequest();
    }
}

demonstration:

public class Demo {
    public static void main(String [] args) {
        Target target = new Adapter();
        target.request(); // result: adapteeRequest method of Adaptee! 
    }
}

7.2 object adapter

From the above two UML diagrams, we can clearly see the difference between the two. The Adapter in the object does not inherit the Adapter, but combines the Adapter as a data member into the class definition, so as to realize the access to its interface.

public class Adapter implements Target {
    private Adaptee adaptee = new Adaptee();
    @Override
    public void request() {
        // TODO Auto-generated method stub
        adaptee.adapteeRequest();
    }
}

7.3 class adapter instance - sorting

Considering the data sorting requirements in a system, let's use the adapter to see how to reuse the existing sorting interface (the following example is just for demonstration, and the interface is not practical);

Existing sort class: EffectiveVectorSort

public class EffectVectorSort {
    public void vectorSort() {
        System.out.println("vectorSort method of EffectVectorSort! ");
    }
}

Interface class definition required for system sorting: DataSort

public interface DataSort {
    void sort();
}

Define adapter: SortAdapter

public class SortAdapter extends EffectVectorSort implements DataSort {
    @Override
    public void sort() {
        // TODO Auto-generated method stub
        super.vectorSort();
    }
}

demonstration:

public class Demo {
    public static void main(String [] args) {
        DataSort dataSort = new SortAdapter();
        dataSort.sort(); // vectorSort method of EffectVectorSort! 
    }
}

7.4 object adapter instance - sorting

If the system not only sorts Vector, but also Tuple, linked list and other advanced data structures, it is obvious that the subclasses of each sorting class cannot be inherited by class adaptation. Here you can use object adaptation.

The SortAdapter in 7.3 needs to be modified, and the sorting class of advanced data structure needs to be defined.

Advanced data structure Sorting class interface: AdvanceDataSort

public interface AdvanceDataSort {
    void sort();
}

LinkListSort: LinkListSort

public class LinkListSort implements AdvanceDataSort {
    @Override
    public void sort() {
        // TODO Auto-generated method stub
        System.out.println("sort method of LinkListSort!");
    }
}

Tuple sort class: TupleSort

public class TupleSort implements AdvanceDataSort {
    @Override
    public void sort() {
        // TODO Auto-generated method stub
        System.out.println("sort method of TupleSort");
    }
}

Redefining adapter: SortAdapter

public class SortAdapter implements DataSort {
    private EffectVectorSort vectorSort = new EffectVectorSort();
    private AdvanceDataSort listSort = new LinkListSort();
    private AdvanceDataSort tupleSort = new TupleSort();
    @Override
    public void sort(String dataType) {
        // TODO Auto-generated method stub
        if(dataType == "vector") {
            vectorSort.vectorSort();
        }
        else if(dataType == "linklist") {
            listSort.sort();
        }
        else if(dataType == "tuple") {
            tupleSort.sort();
        }
        else {
            System.out.println("Invalid Data Type:" + dataType);
        }
    }
}

demonstration:

public class Demo {
    public static void main(String [] args) {
        DataSort dataSort = new SortAdapter();
        dataSort.sort("vector");      // vectorSort method of EffectVectorSort! 
        dataSort.sort("linklist");    // sort method of LinkListSort!
        dataSort.sort("tuple");       // sort method of TupleSort
        dataSort.sort("dict");        // Invalid Data Type:dict
    }
}

Reprint

Reprint statement:
By alpha_panda
Address: https://www.cnblogs.com/yssjun/p/11094401.html

Added by malcolmboston on Sun, 14 Jun 2020 05:26:50 +0300