Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

An overview of the

The adapter pattern is a structural design pattern that transforms the interface of a class into another interface expected by the client so that otherwise incompatible objects can cooperate with each other.

role

Client: The converted object, which is the class that contains the business logic of the current program.

Server: Transformation target, usually from a third party or legacy system, whose interface is incompatible with the existing system and cannot be modified.

Adapter: The core of the adapter pattern. It encapsulates the server object while implementing the client interface. After receiving calls initiated by the client, it converts them into encapsulated service objects and invokes the server.

The template

Client: Class that produces square nails

public class SquarePeg {
    /** * radius */
    private double radius;

    public SquarePeg(a) {}public double getRadius(a) {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public SquarePeg(double radius) {
        this.radius = radius; }}Copy the code

Server: round hole interface and round nails

public class RoundHole {
    /** * radius */
    private double radius;

    /** * Determine whether the nail can pass through the round hole */
    public boolean fits(RoundPeg peg){
        return getRadius() >= peg.getRadius();
    }

    public RoundHole(a) {}public RoundHole(double radius) {
        this.radius = radius;
    }

    public double getRadius(a) {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius; }}Copy the code
public class RoundPeg {
    /** * radius */
    private double radius;

    public RoundPeg(a) {}public RoundPeg(double radius) {
        this.radius = radius;
    }

    public double getRadius(a) {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius; }}Copy the code

Adapter: Converts square nails into round nails

public class SquarePegAdapter extends RoundPeg{
    private SquarePeg squarePeg;

    public SquarePegAdapter(SquarePeg squarePeg) {
        this.squarePeg = squarePeg;
    }

    @Override
    public double getRadius(a) {
        return squarePeg.getRadius() * Math.sqrt(2) / 2; }}Copy the code

Results: Effect display

public class AdapterPattern {
    public static void main(String[] args) {
        // Round hole with radius 5
        RoundHole hole = new RoundHole(5);
        // Round pin with radius 5
        RoundPeg peg = new RoundPeg(5);
        // Square nails with radius 5
        SquarePeg squarePeg = new SquarePeg(5);

        System.out.println("Can a nail of radius 5 pass through a hole of radius 5?" + hole.fits(peg));

        // The input parameter type is abnormal
// system.out. println(" Can a square pin with radius 5 pass through a round hole with radius 5?" + hole.fits(squarePeg));

        / / adapter
        SquarePegAdapter squarePegAdapter = new SquarePegAdapter(squarePeg);
        System.out.println("Can a square nail of radius 5 pass through a round hole of radius 5?"+ hole.fits(squarePegAdapter)); }}Copy the code

summary

advantages

Rule of uniformity: Interface or data transformation logic can be separated from the main business logic of the program.

Open close principle: New adapters can be added without modifying the client code, increasing the flexibility of the code.

disadvantages

The increased complexity required a new set of interfaces and classes.

Applicable scenario

  • Adapter patterns can be used when interfacing with legacy classes, third-party interfaces, or classes that provide other interfaces requires transformation
  • The adapter pattern is used to encapsulate an object that lacks functionality in an adapter when it needs to dynamically obtain the required functionality, similar to the decorator pattern

The last

The article has written the bad place, asks the big guy to be generous to give advice, the mistake is can let the person grow up most, wishes me and the big guy the distance to shorten gradually!!

If you feel the article is helpful to you, please click like, favorites, follow, comment four consecutive support, your support is the biggest power of my creation!!