Object Structural - Adapter Pattern

Adapter Pattern (Adapter Pattern): Convert an interface into another interface that the client wants. The Adapter Pattern enables those classes with incompatible interfaces to work together, and its alias is a wrapper (Wrapper). The adapter pattern can be used as either a class-structural pattern or an object-structural pattern.
Structural Patterns describe how classes or objects can be combined together to form larger structures.

Pattern structure
The adapter pattern includes the following roles:
Target: target abstract class
Adapter: adapter class
Adaptee: adapter class
Client: client class
There are several adapters if there are several adapters.

Pattern Analysis
Typical class adapter code:

public class Adapter extends Adaptee implements Target
{
    public void request()
    {
        specificRequest();
    }
}

Typical object adapter code:

public class Adapter extends Target
{
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee)
    {
        this.adaptee=adaptee;
    }

    public void request()
    {
        adaptee.specificRequest();
    }
} 

In the class adapter mode, the adapter class implements the target abstract class interface and inherits the adapter class, and calls the inherited adapter class methods in the implementation method of the target abstract class; in the object adapter mode, the adapter class The target abstract class is inherited and an object instance of the adapter class is defined, and the corresponding business method of the adapter class is called in the inherited target abstract class method.

Guess you like

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